blob: af877e49b165df61b875aed507b028fe1b0a484a [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
2
Bram Moolenaar24c2e482017-01-29 15:45:12 +01003func Test_empty()
4 call assert_equal(1, empty(''))
5 call assert_equal(0, empty('a'))
6
7 call assert_equal(1, empty(0))
8 call assert_equal(1, empty(-0))
9 call assert_equal(0, empty(1))
10 call assert_equal(0, empty(-1))
11
12 call assert_equal(1, empty(0.0))
13 call assert_equal(1, empty(-0.0))
14 call assert_equal(0, empty(1.0))
15 call assert_equal(0, empty(-1.0))
16 call assert_equal(0, empty(1.0/0.0))
17 call assert_equal(0, empty(0.0/0.0))
18
19 call assert_equal(1, empty([]))
20 call assert_equal(0, empty(['a']))
21
22 call assert_equal(1, empty({}))
23 call assert_equal(0, empty({'a':1}))
24
25 call assert_equal(1, empty(v:null))
26 call assert_equal(1, empty(v:none))
27 call assert_equal(1, empty(v:false))
28 call assert_equal(0, empty(v:true))
29
Bram Moolenaar41042f32017-03-09 12:09:32 +010030 if has('channel')
31 call assert_equal(1, empty(test_null_channel()))
32 endif
33 if has('job')
34 call assert_equal(1, empty(test_null_job()))
35 endif
36
Bram Moolenaar24c2e482017-01-29 15:45:12 +010037 call assert_equal(0, empty(function('Test_empty')))
38endfunc
39
40func Test_len()
41 call assert_equal(1, len(0))
42 call assert_equal(2, len(12))
43
44 call assert_equal(0, len(''))
45 call assert_equal(2, len('ab'))
46
47 call assert_equal(0, len([]))
48 call assert_equal(2, len([2, 1]))
49
50 call assert_equal(0, len({}))
51 call assert_equal(2, len({'a': 1, 'b': 2}))
52
53 call assert_fails('call len(v:none)', 'E701:')
54 call assert_fails('call len({-> 0})', 'E701:')
55endfunc
56
57func Test_max()
58 call assert_equal(0, max([]))
59 call assert_equal(2, max([2]))
60 call assert_equal(2, max([1, 2]))
61 call assert_equal(2, max([1, 2, v:null]))
62
63 call assert_equal(0, max({}))
64 call assert_equal(2, max({'a':1, 'b':2}))
65
66 call assert_fails('call max(1)', 'E712:')
67 call assert_fails('call max(v:none)', 'E712:')
68endfunc
69
70func Test_min()
71 call assert_equal(0, min([]))
72 call assert_equal(2, min([2]))
73 call assert_equal(1, min([1, 2]))
74 call assert_equal(0, min([1, 2, v:null]))
75
76 call assert_equal(0, min({}))
77 call assert_equal(1, min({'a':1, 'b':2}))
78
79 call assert_fails('call min(1)', 'E712:')
80 call assert_fails('call min(v:none)', 'E712:')
81endfunc
82
Bram Moolenaar08243d22017-01-10 16:12:29 +010083func Test_str2nr()
84 call assert_equal(0, str2nr(''))
85 call assert_equal(1, str2nr('1'))
86 call assert_equal(1, str2nr(' 1 '))
87
88 call assert_equal(1, str2nr('+1'))
89 call assert_equal(1, str2nr('+ 1'))
90 call assert_equal(1, str2nr(' + 1 '))
91
92 call assert_equal(-1, str2nr('-1'))
93 call assert_equal(-1, str2nr('- 1'))
94 call assert_equal(-1, str2nr(' - 1 '))
95
96 call assert_equal(123456789, str2nr('123456789'))
97 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010098
99 call assert_equal(5, str2nr('101', 2))
100 call assert_equal(5, str2nr('0b101', 2))
101 call assert_equal(5, str2nr('0B101', 2))
102 call assert_equal(-5, str2nr('-101', 2))
103 call assert_equal(-5, str2nr('-0b101', 2))
104 call assert_equal(-5, str2nr('-0B101', 2))
105
106 call assert_equal(65, str2nr('101', 8))
107 call assert_equal(65, str2nr('0101', 8))
108 call assert_equal(-65, str2nr('-101', 8))
109 call assert_equal(-65, str2nr('-0101', 8))
110
111 call assert_equal(11259375, str2nr('abcdef', 16))
112 call assert_equal(11259375, str2nr('ABCDEF', 16))
113 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
114 call assert_equal(11259375, str2nr('0xabcdef', 16))
115 call assert_equal(11259375, str2nr('0Xabcdef', 16))
116 call assert_equal(11259375, str2nr('0XABCDEF', 16))
117 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
118
119 call assert_equal(0, str2nr('0x10'))
120 call assert_equal(0, str2nr('0b10'))
121 call assert_equal(1, str2nr('12', 2))
122 call assert_equal(1, str2nr('18', 8))
123 call assert_equal(1, str2nr('1g', 16))
124
125 call assert_equal(0, str2nr(v:null))
126 call assert_equal(0, str2nr(v:none))
127
128 call assert_fails('call str2nr([])', 'E730:')
129 call assert_fails('call str2nr({->2})', 'E729:')
130 call assert_fails('call str2nr(1.2)', 'E806:')
131 call assert_fails('call str2nr(10, [])', 'E474:')
132endfunc
133
134func Test_strftime()
135 if !exists('*strftime')
136 return
137 endif
138 " Format of strftime() depends on system. We assume
139 " that basic formats tested here are available and
140 " identical on all systems which support strftime().
141 "
142 " The 2nd parameter of strftime() is a local time, so the output day
143 " of strftime() can be 17 or 18, depending on timezone.
144 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
145 "
146 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'))
147
148 call assert_fails('call strftime([])', 'E730:')
149 call assert_fails('call strftime("%Y", [])', 'E745:')
150endfunc
151
152func Test_simplify()
153 call assert_equal('', simplify(''))
154 call assert_equal('/', simplify('/'))
155 call assert_equal('/', simplify('/.'))
156 call assert_equal('/', simplify('/..'))
157 call assert_equal('/...', simplify('/...'))
158 call assert_equal('./dir/file', simplify('./dir/file'))
159 call assert_equal('./dir/file', simplify('.///dir//file'))
160 call assert_equal('./dir/file', simplify('./dir/./file'))
161 call assert_equal('./file', simplify('./dir/../file'))
162 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
163 call assert_equal('./file', simplify('dir/.././file'))
164
165 call assert_fails('call simplify({->0})', 'E729:')
166 call assert_fails('call simplify([])', 'E730:')
167 call assert_fails('call simplify({})', 'E731:')
168 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100169endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100170
171func Test_tolower()
172 call assert_equal("", tolower(""))
173
174 " Test with all printable ASCII characters.
175 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
176 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
177
178 if !has('multi_byte')
179 return
180 endif
181
182 " Test with a few uppercase diacritics.
183 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
184 call assert_equal("bḃḇ", tolower("BḂḆ"))
185 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
186 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
187 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
188 call assert_equal("fḟ ", tolower("FḞ "))
189 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
190 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
191 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
192 call assert_equal("jĵ", tolower("JĴ"))
193 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
194 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
195 call assert_equal("mḿṁ", tolower("MḾṀ"))
196 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
197 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
198 call assert_equal("pṕṗ", tolower("PṔṖ"))
199 call assert_equal("q", tolower("Q"))
200 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
201 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
202 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
203 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
204 call assert_equal("vṽ", tolower("VṼ"))
205 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
206 call assert_equal("xẋẍ", tolower("XẊẌ"))
207 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
208 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
209
210 " Test with a few lowercase diacritics, which should remain unchanged.
211 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
212 call assert_equal("bḃḇ", tolower("bḃḇ"))
213 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
214 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
215 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
216 call assert_equal("fḟ", tolower("fḟ"))
217 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
218 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
219 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
220 call assert_equal("jĵǰ", tolower("jĵǰ"))
221 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
222 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
223 call assert_equal("mḿṁ ", tolower("mḿṁ "))
224 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
225 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
226 call assert_equal("pṕṗ", tolower("pṕṗ"))
227 call assert_equal("q", tolower("q"))
228 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
229 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
230 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
231 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
232 call assert_equal("vṽ", tolower("vṽ"))
233 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
234 call assert_equal("ẋẍ", tolower("ẋẍ"))
235 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
236 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
237
238 " According to https://twitter.com/jifa/status/625776454479970304
239 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
240 " in length (2 to 3 bytes) when lowercased. So let's test them.
241 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
242endfunc
243
244func Test_toupper()
245 call assert_equal("", toupper(""))
246
247 " Test with all printable ASCII characters.
248 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
249 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
250
251 if !has('multi_byte')
252 return
253 endif
254
255 " Test with a few lowercase diacritics.
256 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
257 call assert_equal("BḂḆ", toupper("bḃḇ"))
258 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
259 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
260 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
261 call assert_equal("FḞ", toupper("fḟ"))
262 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
263 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
264 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
265 call assert_equal("JĴǰ", toupper("jĵǰ"))
266 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
267 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
268 call assert_equal("MḾṀ ", toupper("mḿṁ "))
269 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
270 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
271 call assert_equal("PṔṖ", toupper("pṕṗ"))
272 call assert_equal("Q", toupper("q"))
273 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
274 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
275 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
276 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
277 call assert_equal("VṼ", toupper("vṽ"))
278 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
279 call assert_equal("ẊẌ", toupper("ẋẍ"))
280 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
281 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
282
283 " Test that uppercase diacritics, which should remain unchanged.
284 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
285 call assert_equal("BḂḆ", toupper("BḂḆ"))
286 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
287 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
288 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
289 call assert_equal("FḞ ", toupper("FḞ "))
290 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
291 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
292 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
293 call assert_equal("JĴ", toupper("JĴ"))
294 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
295 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
296 call assert_equal("MḾṀ", toupper("MḾṀ"))
297 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
298 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
299 call assert_equal("PṔṖ", toupper("PṔṖ"))
300 call assert_equal("Q", toupper("Q"))
301 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
302 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
303 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
304 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
305 call assert_equal("VṼ", toupper("VṼ"))
306 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
307 call assert_equal("XẊẌ", toupper("XẊẌ"))
308 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
309 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
310
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100311 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100312endfunc
313
Bram Moolenaare90858d2017-02-01 17:24:34 +0100314" Tests for the mode() function
315let current_modes = ''
316func! Save_mode()
317 let g:current_modes = mode(0) . '-' . mode(1)
318 return ''
319endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100320
Bram Moolenaare90858d2017-02-01 17:24:34 +0100321func! Test_mode()
322 new
323 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
324
325 inoremap <F2> <C-R>=Save_mode()<CR>
326
327 normal! 3G
328 exe "normal i\<F2>\<Esc>"
329 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100330 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100331 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
332 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100333 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100334 exe "normal iBro\<C-P>\<F2>\<Esc>u"
335 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100336 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100337 exe "normal iBa\<C-X>\<F2>\<Esc>u"
338 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100339 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100340 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
341 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100342 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100343 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
344 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100345 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100346 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
347 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100348 " i_CTRL-X CTRL-L: Multiple matches
349 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
350 call assert_equal('i-ic', g:current_modes)
351 " i_CTRL-X CTRL-L: Single match
352 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
353 call assert_equal('i-ic', g:current_modes)
354 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100355 exe "normal iCom\<C-P>\<F2>\<Esc>u"
356 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100357 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100358 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
359 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100360 " i_CTRL-X CTRL-L: No match
361 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
362 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100363
Bram Moolenaare971df32017-02-05 14:15:29 +0100364 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100365 exe "normal RBa\<C-P>\<F2>\<Esc>u"
366 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100367 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100368 exe "normal RBro\<C-P>\<F2>\<Esc>u"
369 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100370 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100371 exe "normal RBa\<C-X>\<F2>\<Esc>u"
372 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100373 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100374 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
375 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100376 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100377 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
378 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100379 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100380 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
381 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100382 " R_CTRL-X CTRL-L: Multiple matches
383 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
384 call assert_equal('R-Rc', g:current_modes)
385 " R_CTRL-X CTRL-L: Single match
386 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
387 call assert_equal('R-Rc', g:current_modes)
388 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100389 exe "normal RCom\<C-P>\<F2>\<Esc>u"
390 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100391 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100392 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
393 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100394 " R_CTRL-X CTRL-L: No match
395 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
396 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100397
398 call assert_equal('n', mode(0))
399 call assert_equal('n', mode(1))
400
401 " How to test operator-pending mode?
402
403 call feedkeys("v", 'xt')
404 call assert_equal('v', mode())
405 call assert_equal('v', mode(1))
406 call feedkeys("\<Esc>V", 'xt')
407 call assert_equal('V', mode())
408 call assert_equal('V', mode(1))
409 call feedkeys("\<Esc>\<C-V>", 'xt')
410 call assert_equal("\<C-V>", mode())
411 call assert_equal("\<C-V>", mode(1))
412 call feedkeys("\<Esc>", 'xt')
413
414 call feedkeys("gh", 'xt')
415 call assert_equal('s', mode())
416 call assert_equal('s', mode(1))
417 call feedkeys("\<Esc>gH", 'xt')
418 call assert_equal('S', mode())
419 call assert_equal('S', mode(1))
420 call feedkeys("\<Esc>g\<C-H>", 'xt')
421 call assert_equal("\<C-S>", mode())
422 call assert_equal("\<C-S>", mode(1))
423 call feedkeys("\<Esc>", 'xt')
424
425 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
426 call assert_equal('c-c', g:current_modes)
427 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
428 call assert_equal('c-cv', g:current_modes)
429 " How to test Ex mode?
430
431 bwipe!
432 iunmap <F2>
433endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100434
435func Test_getbufvar()
436 let bnr = bufnr('%')
437 let b:var_num = '1234'
438 let def_num = '5678'
439 call assert_equal('1234', getbufvar(bnr, 'var_num'))
440 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
441
442 let bd = getbufvar(bnr, '')
443 call assert_equal('1234', bd['var_num'])
444 call assert_true(exists("bd['changedtick']"))
445 call assert_equal(2, len(bd))
446
447 let bd2 = getbufvar(bnr, '', def_num)
448 call assert_equal(bd, bd2)
449
450 unlet b:var_num
451 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
452 call assert_equal('', getbufvar(bnr, 'var_num'))
453
454 let bd = getbufvar(bnr, '')
455 call assert_equal(1, len(bd))
456 let bd = getbufvar(bnr, '',def_num)
457 call assert_equal(1, len(bd))
458
459 call assert_equal('', getbufvar(9, ''))
460 call assert_equal(def_num, getbufvar(9, '', def_num))
461 unlet def_num
462
Bram Moolenaar507647d2017-02-17 16:43:49 +0100463 call assert_equal(0, getbufvar(bnr, '&autoindent'))
464 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100465
466 " Open new window with forced option values
467 set fileformats=unix,dos
468 new ++ff=dos ++bin ++enc=iso-8859-2
469 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
470 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
471 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
472 close
473
474 set fileformats&
475endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100476
Bram Moolenaar41042f32017-03-09 12:09:32 +0100477func Test_bufexists()
478 call assert_equal(0, bufexists('does_not_exist'))
479 call assert_equal(1, bufexists(bufnr('%')))
480 call assert_equal(0, bufexists(0))
481 new Xfoo
482 let bn = bufnr('%')
483 call assert_equal(1, bufexists(bn))
484 call assert_equal(1, bufexists('Xfoo'))
485 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
486 call assert_equal(1, bufexists(0))
487 bw
488 call assert_equal(0, bufexists(bn))
489 call assert_equal(0, bufexists('Xfoo'))
490endfunc
491
492func Test_last_buffer_nr()
493 call assert_equal(bufnr('$'), last_buffer_nr())
494endfunc
495
496func Test_stridx()
497 call assert_equal(-1, stridx('', 'l'))
498 call assert_equal(0, stridx('', ''))
499 call assert_equal(0, stridx('hello', ''))
500 call assert_equal(-1, stridx('hello', 'L'))
501 call assert_equal(2, stridx('hello', 'l', -1))
502 call assert_equal(2, stridx('hello', 'l', 0))
503 call assert_equal(2, stridx('hello', 'l', 1))
504 call assert_equal(3, stridx('hello', 'l', 3))
505 call assert_equal(-1, stridx('hello', 'l', 4))
506 call assert_equal(-1, stridx('hello', 'l', 10))
507 call assert_equal(2, stridx('hello', 'll'))
508 call assert_equal(-1, stridx('hello', 'hello world'))
509endfunc
510
511func Test_strridx()
512 call assert_equal(-1, strridx('', 'l'))
513 call assert_equal(0, strridx('', ''))
514 call assert_equal(5, strridx('hello', ''))
515 call assert_equal(-1, strridx('hello', 'L'))
516 call assert_equal(3, strridx('hello', 'l'))
517 call assert_equal(3, strridx('hello', 'l', 10))
518 call assert_equal(3, strridx('hello', 'l', 3))
519 call assert_equal(2, strridx('hello', 'l', 2))
520 call assert_equal(-1, strridx('hello', 'l', 1))
521 call assert_equal(-1, strridx('hello', 'l', 0))
522 call assert_equal(-1, strridx('hello', 'l', -1))
523 call assert_equal(2, strridx('hello', 'll'))
524 call assert_equal(-1, strridx('hello', 'hello world'))
525endfunc
526
527func Test_matchend()
528 call assert_equal(7, matchend('testing', 'ing'))
529 call assert_equal(7, matchend('testing', 'ing', 2))
530 call assert_equal(-1, matchend('testing', 'ing', 5))
531endfunc
532
533func Test_nextnonblank_prevnonblank()
534 new
535insert
536This
537
538
539is
540
541a
542Test
543.
544 call assert_equal(0, nextnonblank(-1))
545 call assert_equal(0, nextnonblank(0))
546 call assert_equal(1, nextnonblank(1))
547 call assert_equal(4, nextnonblank(2))
548 call assert_equal(4, nextnonblank(3))
549 call assert_equal(4, nextnonblank(4))
550 call assert_equal(6, nextnonblank(5))
551 call assert_equal(6, nextnonblank(6))
552 call assert_equal(7, nextnonblank(7))
553 call assert_equal(0, nextnonblank(8))
554
555 call assert_equal(0, prevnonblank(-1))
556 call assert_equal(0, prevnonblank(0))
557 call assert_equal(1, prevnonblank(1))
558 call assert_equal(1, prevnonblank(2))
559 call assert_equal(1, prevnonblank(3))
560 call assert_equal(4, prevnonblank(4))
561 call assert_equal(4, prevnonblank(5))
562 call assert_equal(6, prevnonblank(6))
563 call assert_equal(7, prevnonblank(7))
564 call assert_equal(0, prevnonblank(8))
565 bw!
566endfunc
567
568func Test_byte2line_line2byte()
569 new
570 call setline(1, ['a', 'bc', 'd'])
571
572 set fileformat=unix
573 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
574 \ map(range(-1, 8), 'byte2line(v:val)'))
575 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
576 \ map(range(-1, 5), 'line2byte(v:val)'))
577
578 set fileformat=mac
579 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
580 \ map(range(-1, 8), 'byte2line(v:val)'))
581 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
582 \ map(range(-1, 5), 'line2byte(v:val)'))
583
584 set fileformat=dos
585 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
586 \ map(range(-1, 11), 'byte2line(v:val)'))
587 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
588 \ map(range(-1, 5), 'line2byte(v:val)'))
589
590 set fileformat&
591 bw!
592endfunc
593
594func Test_count()
595 let l = ['a', 'a', 'A', 'b']
596 call assert_equal(2, count(l, 'a'))
597 call assert_equal(1, count(l, 'A'))
598 call assert_equal(1, count(l, 'b'))
599 call assert_equal(0, count(l, 'B'))
600
601 call assert_equal(2, count(l, 'a', 0))
602 call assert_equal(1, count(l, 'A', 0))
603 call assert_equal(1, count(l, 'b', 0))
604 call assert_equal(0, count(l, 'B', 0))
605
606 call assert_equal(3, count(l, 'a', 1))
607 call assert_equal(3, count(l, 'A', 1))
608 call assert_equal(1, count(l, 'b', 1))
609 call assert_equal(1, count(l, 'B', 1))
610 call assert_equal(0, count(l, 'c', 1))
611
612 call assert_equal(1, count(l, 'a', 0, 1))
613 call assert_equal(2, count(l, 'a', 1, 1))
614 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
615
616 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
617 call assert_equal(2, count(d, 'a'))
618 call assert_equal(1, count(d, 'A'))
619 call assert_equal(1, count(d, 'b'))
620 call assert_equal(0, count(d, 'B'))
621
622 call assert_equal(2, count(d, 'a', 0))
623 call assert_equal(1, count(d, 'A', 0))
624 call assert_equal(1, count(d, 'b', 0))
625 call assert_equal(0, count(d, 'B', 0))
626
627 call assert_equal(3, count(d, 'a', 1))
628 call assert_equal(3, count(d, 'A', 1))
629 call assert_equal(1, count(d, 'b', 1))
630 call assert_equal(1, count(d, 'B', 1))
631 call assert_equal(0, count(d, 'c', 1))
632
633 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
634 call assert_fails('call count("a", "a")', 'E712:')
635endfunc
636
637func Test_changenr()
638 new Xchangenr
639 call assert_equal(0, changenr())
640 norm ifoo
641 call assert_equal(1, changenr())
642 set undolevels=10
643 norm Sbar
644 call assert_equal(2, changenr())
645 undo
646 call assert_equal(1, changenr())
647 redo
648 call assert_equal(2, changenr())
649 bw!
650 set undolevels&
651endfunc
652
653func Test_filewritable()
654 new Xfilewritable
655 write!
656 call assert_equal(1, filewritable('Xfilewritable'))
657
658 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
659 call assert_equal(0, filewritable('Xfilewritable'))
660
661 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
662 call assert_equal(1, filewritable('Xfilewritable'))
663
664 call assert_equal(0, filewritable('doesnotexist'))
665
666 call delete('Xfilewritable')
667 bw!
668endfunc
669
670func Test_hostname()
671 let hostname_vim = hostname()
672 if has('unix')
673 let hostname_system = systemlist('uname -n')[0]
674 call assert_equal(hostname_vim, hostname_system)
675 endif
676endfunc
677
678func Test_getpid()
679 " getpid() always returns the same value within a vim instance.
680 call assert_equal(getpid(), getpid())
681 if has('unix')
682 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
683 endif
684endfunc
685
686func Test_hlexists()
687 call assert_equal(0, hlexists('does_not_exist'))
688 call assert_equal(0, hlexists('Number'))
689 call assert_equal(0, highlight_exists('does_not_exist'))
690 call assert_equal(0, highlight_exists('Number'))
691 syntax on
692 call assert_equal(0, hlexists('does_not_exist'))
693 call assert_equal(1, hlexists('Number'))
694 call assert_equal(0, highlight_exists('does_not_exist'))
695 call assert_equal(1, highlight_exists('Number'))
696 syntax off
697endfunc
698
699func Test_col()
700 new
701 call setline(1, 'abcdef')
702 norm gg4|mx6|mY2|
703 call assert_equal(2, col('.'))
704 call assert_equal(7, col('$'))
705 call assert_equal(4, col("'x"))
706 call assert_equal(6, col("'Y"))
707 call assert_equal(2, col([1, 2]))
708 call assert_equal(7, col([1, '$']))
709
710 call assert_equal(0, col(''))
711 call assert_equal(0, col('x'))
712 call assert_equal(0, col([2, '$']))
713 call assert_equal(0, col([1, 100]))
714 call assert_equal(0, col([1]))
715 bw!
716endfunc
717
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100718func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +0100719 if has('balloon_eval')
720 " This won't do anything but must not crash either.
721 call balloon_show('hi!')
722 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100723endfunc