robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
37.2 KiB959 linesraw
1" Tests for expressions.
2
3source check.vim
4source vim9.vim
5
6func Test_equal()
7  let base = {}
8  func base.method()
9    return 1
10  endfunc
11  func base.other() dict
12    return 1
13  endfunc
14  let instance = copy(base)
15  call assert_true(base.method == instance.method)
16  call assert_true([base.method] == [instance.method])
17  call assert_true(base.other == instance.other)
18  call assert_true([base.other] == [instance.other])
19
20  call assert_false(base.method == base.other)
21  call assert_false([base.method] == [base.other])
22  call assert_false(base.method == instance.other)
23  call assert_false([base.method] == [instance.other])
24
25  call assert_fails('echo base.method > instance.method', 'E694: Invalid operation for Funcrefs')
26  " Nvim doesn't have null functions
27  " call assert_equal(0, test_null_function() == function('min'))
28  " call assert_equal(1, test_null_function() == test_null_function())
29  " Nvim doesn't have test_unknown()
30  " call assert_fails('eval 10 == test_unknown()', 'E685:')
31endfunc
32
33func Test_version()
34  call assert_true(has('patch-7.4.001'))
35  call assert_true(has('patch-7.4.01'))
36  call assert_true(has('patch-7.4.1'))
37  call assert_true(has('patch-6.9.999'))
38  call assert_true(has('patch-7.1.999'))
39  call assert_true(has('patch-7.4.123'))
40  call assert_true(has('patch-7.4.123 ')) " Trailing space can be allowed.
41  call assert_true(has('patch-9.1.0'))
42  call assert_true(has('patch-9.1.0000'))
43  call assert_true(has('patch-9.2.0'))
44  call assert_true(has('patch-9.2.0000'))
45
46  call assert_false(has('patch-7'))
47  call assert_false(has('patch-7.4'))
48  call assert_false(has('patch-7.4.'))
49  call assert_false(has('patch-9.3.0'))
50  call assert_false(has('patch-9.9.1'))
51
52  call assert_false(has('patch-abc'))
53  call assert_false(has('patchabc'))
54
55  call assert_false(has('patch-8x001'))
56  call assert_false(has('patch-9X0X0'))
57  call assert_false(has('patch-9-0-0'))
58  call assert_false(has('patch-09.0.0'))
59  call assert_false(has('patch-9.00.0'))
60endfunc
61
62func Test_op_ternary()
63  let lines =<< trim END
64      call assert_equal('yes', 1 ? 'yes' : 'no')
65      call assert_equal('no', 0 ? 'yes' : 'no')
66
67      call assert_fails('echo [1] ? "yes" : "no"', 'E745:')
68      call assert_fails('echo {} ? "yes" : "no"', 'E728:')
69  END
70  call CheckLegacyAndVim9Success(lines)
71
72  call assert_equal('no', 'x' ? 'yes' : 'no')
73  call CheckDefAndScriptFailure(["'x' ? 'yes' : 'no'"], 'E1135:')
74  call assert_equal('yes', '1x' ? 'yes' : 'no')
75  call CheckDefAndScriptFailure(["'1x' ? 'yes' : 'no'"], 'E1135:')
76endfunc
77
78func Test_op_falsy()
79  let lines =<< trim END
80      call assert_equal(v:true, v:true ?? 456)
81      call assert_equal(123, 123 ?? 456)
82      call assert_equal('yes', 'yes' ?? 456)
83      call assert_equal(0z00, 0z00 ?? 456)
84      call assert_equal([1], [1] ?? 456)
85      call assert_equal({'one': 1}, {'one': 1} ?? 456)
86      call assert_equal(0.1, 0.1 ?? 456)
87
88      call assert_equal(456, v:false ?? 456)
89      call assert_equal(456, 0 ?? 456)
90      call assert_equal(456, '' ?? 456)
91      call assert_equal(456, 0z ?? 456)
92      call assert_equal(456, [] ?? 456)
93      call assert_equal(456, {} ?? 456)
94      call assert_equal(456, 0.0 ?? 456)
95
96      call assert_equal(456, v:null ?? 456)
97      #" call assert_equal(456, v:none ?? 456)
98      call assert_equal(456, v:_null_string ?? 456)
99      call assert_equal(456, v:_null_blob ?? 456)
100      call assert_equal(456, v:_null_list ?? 456)
101      call assert_equal(456, v:_null_dict ?? 456)
102      #" Nvim doesn't have null functions
103      #" call assert_equal(456, test_null_function() ?? 456)
104      #" Nvim doesn't have null partials
105      #" call assert_equal(456, test_null_partial() ?? 456)
106      if has('job')
107        call assert_equal(456, test_null_job() ?? 456)
108      endif
109      if has('channel')
110        call assert_equal(456, test_null_channel() ?? 456)
111      endif
112  END
113  call CheckLegacyAndVim9Success(lines)
114endfunc
115
116func Test_dict()
117  let lines =<< trim END
118      VAR d = {'': 'empty', 'a': 'a', 0: 'zero'}
119      call assert_equal('empty', d[''])
120      call assert_equal('a', d['a'])
121      call assert_equal('zero', d[0])
122      call assert_true(has_key(d, ''))
123      call assert_true(has_key(d, 'a'))
124
125      LET d[''] = 'none'
126      LET d['a'] = 'aaa'
127      call assert_equal('none', d[''])
128      call assert_equal('aaa', d['a'])
129
130      LET d[ 'b' ] = 'bbb'
131      call assert_equal('bbb', d[ 'b' ])
132  END
133  call CheckLegacyAndVim9Success(lines)
134
135  call CheckLegacyAndVim9Failure(["VAR i = has_key([], 'a')"], ['E1206:', 'E1013:', 'E1206:'])
136endfunc
137
138func Test_strgetchar()
139  let lines =<< trim END
140      call assert_equal(char2nr('a'), strgetchar('axb', 0))
141      call assert_equal(char2nr('x'), 'axb'->strgetchar(1))
142      call assert_equal(char2nr('b'), strgetchar('axb', 2))
143
144      call assert_equal(-1, strgetchar('axb', -1))
145      call assert_equal(-1, strgetchar('axb', 3))
146      call assert_equal(-1, strgetchar('', 0))
147  END
148  call CheckLegacyAndVim9Success(lines)
149
150  call CheckLegacyAndVim9Failure(["VAR c = strgetchar([], 1)"], ['E730:', 'E1013:', 'E1174:'])
151  call CheckLegacyAndVim9Failure(["VAR c = strgetchar('axb', [])"], ['E745:', 'E1013:', 'E1210:'])
152endfunc
153
154func Test_strcharpart()
155  let lines =<< trim END
156      call assert_equal('a', strcharpart('axb', 0, 1))
157      call assert_equal('x', 'axb'->strcharpart(1, 1))
158      call assert_equal('b', strcharpart('axb', 2, 1))
159      call assert_equal('xb', strcharpart('axb', 1))
160
161      call assert_equal('', strcharpart('axb', 1, 0))
162      call assert_equal('', strcharpart('axb', 1, -1))
163      call assert_equal('', strcharpart('axb', -1, 1))
164      call assert_equal('', strcharpart('axb', -2, 2))
165
166      call assert_equal('a', strcharpart('axb', -1, 2))
167
168      call assert_equal('edit', "editor"[-10 : 3])
169  END
170  call CheckLegacyAndVim9Success(lines)
171
172  call assert_fails('call strcharpart("", 0, 0, {})', ['E728:', 'E728:'])
173  call assert_fails('call strcharpart("", 0, 0, -1)', ['E1023:', 'E1023:'])
174endfunc
175
176func Test_getreg_empty_list()
177  let lines =<< trim END
178      call assert_equal('', getreg('x'))
179      call assert_equal([], getreg('x', 1, 1))
180      VAR x = getreg('x', 1, 1)
181      VAR y = x
182      call add(x, 'foo')
183      call assert_equal(['foo'], y)
184  END
185  call CheckLegacyAndVim9Success(lines)
186
187  call CheckLegacyAndVim9Failure(['call getreg([])'], ['E730:', 'E1013:', 'E1174:'])
188endfunc
189
190func Test_loop_over_null_list()
191  let lines =<< trim END
192      VAR null_list = v:_null_list
193      for i in null_list
194        call assert_report('should not get here')
195      endfor
196  END
197  call CheckLegacyAndVim9Success(lines)
198endfunc
199
200func Test_setreg_null_list()
201  let lines =<< trim END
202      call setreg('x', v:_null_list)
203  END
204  call CheckLegacyAndVim9Success(lines)
205endfunc
206
207func Test_special_char()
208  " The failure is only visible using valgrind.
209  call CheckLegacyAndVim9Failure(['echo "\<C-">'], ['E15:', 'E1004:', 'E1004:'])
210endfunc
211
212func Test_method_with_prefix()
213  let lines =<< trim END
214      call assert_equal(TRUE, !range(5)->empty())
215      call assert_equal(FALSE, !-3)
216  END
217  call CheckLegacyAndVim9Success(lines)
218
219  call assert_equal([0, 1, 2], --3->range())
220  call CheckDefAndScriptFailure(['eval --3->range()'], 'E15:')
221
222  call assert_equal(1, !+-+0)
223  call CheckDefAndScriptFailure(['eval !+-+0'], 'E15:')
224endfunc
225
226func Test_option_value()
227  let lines =<< trim END
228      #" boolean
229      set bri
230      call assert_equal(TRUE, &bri)
231      set nobri
232      call assert_equal(FALSE, &bri)
233
234      #" number
235      set ts=1
236      call assert_equal(1, &ts)
237      set ts=8
238      call assert_equal(8, &ts)
239
240      #" string
241      exe "set cedit=\<Esc>"
242      call assert_equal("\<Esc>", &cedit)
243      set cpo=
244      call assert_equal("", &cpo)
245      set cpo=abcdefi
246      call assert_equal("abcdefi", &cpo)
247      set cpo&vim
248  END
249  call CheckLegacyAndVim9Success(lines)
250endfunc
251
252func Test_printf_misc()
253  let lines =<< trim END
254      call assert_equal('123', printf('123'))
255
256      call assert_equal('', printf('%'))
257      call assert_equal('', printf('%.0d', 0))
258      call assert_equal('123', printf('%d', 123))
259      call assert_equal('123', printf('%i', 123))
260      call assert_equal('123', printf('%D', 123))
261      call assert_equal('123', printf('%U', 123))
262      call assert_equal('173', printf('%o', 123))
263      call assert_equal('173', printf('%O', 123))
264      call assert_equal('7b', printf('%x', 123))
265      call assert_equal('7B', printf('%X', 123))
266
267      call assert_equal('123', printf('%hd', 123))
268      call assert_equal('-123', printf('%hd', -123))
269      call assert_equal('-1', printf('%hd', 0xFFFF))
270      call assert_equal('-1', printf('%hd', 0x1FFFFF))
271
272      call assert_equal('123', printf('%hu', 123))
273      call assert_equal('65413', printf('%hu', -123))
274      call assert_equal('65535', printf('%hu', 0xFFFF))
275      call assert_equal('65535', printf('%hu', 0x1FFFFF))
276
277      call assert_equal('123', printf('%ld', 123))
278      call assert_equal('-123', printf('%ld', -123))
279      call assert_equal('65535', printf('%ld', 0xFFFF))
280      call assert_equal('131071', printf('%ld', 0x1FFFF))
281
282      call assert_equal('{', printf('%c', 123))
283      call assert_equal('abc', printf('%s', 'abc'))
284      call assert_equal('abc', printf('%S', 'abc'))
285
286      call assert_equal('+123', printf('%+d', 123))
287      call assert_equal('-123', printf('%+d', -123))
288      call assert_equal('+123', printf('%+ d', 123))
289      call assert_equal(' 123', printf('% d', 123))
290      call assert_equal(' 123', printf('%  d', 123))
291      call assert_equal('-123', printf('% d', -123))
292
293      call assert_equal('123', printf('%2d', 123))
294      call assert_equal('   123', printf('%6d', 123))
295      call assert_equal('000123', printf('%06d', 123))
296      call assert_equal('+00123', printf('%+06d', 123))
297      call assert_equal(' 00123', printf('% 06d', 123))
298      call assert_equal('  +123', printf('%+6d', 123))
299      call assert_equal('   123', printf('% 6d', 123))
300      call assert_equal('  -123', printf('% 6d', -123))
301
302      #" Test left adjusted.
303      call assert_equal('123   ', printf('%-6d', 123))
304      call assert_equal('+123  ', printf('%-+6d', 123))
305      call assert_equal(' 123  ', printf('%- 6d', 123))
306      call assert_equal('-123  ', printf('%- 6d', -123))
307
308      call assert_equal('  00123', printf('%7.5d', 123))
309      call assert_equal(' -00123', printf('%7.5d', -123))
310      call assert_equal(' +00123', printf('%+7.5d', 123))
311
312      #" Precision field should not be used when combined with %0
313      call assert_equal('  00123', printf('%07.5d', 123))
314      call assert_equal(' -00123', printf('%07.5d', -123))
315
316      call assert_equal('  123', printf('%*d', 5, 123))
317      call assert_equal('123  ', printf('%*d', -5, 123))
318      call assert_equal('00123', printf('%.*d', 5, 123))
319      call assert_equal('  123', printf('% *d', 5, 123))
320      call assert_equal(' +123', printf('%+ *d', 5, 123))
321
322      call assert_equal('foobar', printf('%.*s',  9, 'foobar'))
323      call assert_equal('foo',    printf('%.*s',  3, 'foobar'))
324      call assert_equal('',       printf('%.*s',  0, 'foobar'))
325      call assert_equal('foobar', printf('%.*s', -1, 'foobar'))
326
327      #" Simple quote (thousand grouping char) is ignored.
328      call assert_equal('+00123456', printf("%+'09d", 123456))
329
330      #" Unrecognized format specifier kept as-is.
331      call assert_equal('_123', printf("%_%d", 123))
332
333      #" Test alternate forms.
334      call assert_equal('0x7b', printf('%#x', 123))
335      call assert_equal('0X7B', printf('%#X', 123))
336      call assert_equal('0173', printf('%#o', 123))
337      call assert_equal('0173', printf('%#O', 123))
338      call assert_equal('abc', printf('%#s', 'abc'))
339      call assert_equal('abc', printf('%#S', 'abc'))
340      call assert_equal('  0173', printf('%#6o', 123))
341      call assert_equal(' 00173', printf('%#6.5o', 123))
342      call assert_equal('  0173', printf('%#6.2o', 123))
343      call assert_equal('  0173', printf('%#6.2o', 123))
344      call assert_equal('0173', printf('%#2.2o', 123))
345
346      call assert_equal(' 00123', printf('%6.5d', 123))
347      call assert_equal(' 0007b', printf('%6.5x', 123))
348
349      call assert_equal('123', printf('%.2d', 123))
350      call assert_equal('0123', printf('%.4d', 123))
351      call assert_equal('0000000123', printf('%.10d', 123))
352      call assert_equal('123', printf('%.0d', 123))
353
354      call assert_equal('abc', printf('%2s', 'abc'))
355      call assert_equal('abc', printf('%2S', 'abc'))
356      call assert_equal('abc', printf('%.4s', 'abc'))
357      call assert_equal('abc', printf('%.4S', 'abc'))
358      call assert_equal('ab', printf('%.2s', 'abc'))
359      call assert_equal('ab', printf('%.2S', 'abc'))
360      call assert_equal('', printf('%.0s', 'abc'))
361      call assert_equal('', printf('%.s', 'abc'))
362      call assert_equal(' abc', printf('%4s', 'abc'))
363      call assert_equal(' abc', printf('%4S', 'abc'))
364      call assert_equal('0abc', printf('%04s', 'abc'))
365      call assert_equal('0abc', printf('%04S', 'abc'))
366      call assert_equal('abc ', printf('%-4s', 'abc'))
367      call assert_equal('abc ', printf('%-4S', 'abc'))
368
369      call assert_equal('🐍', printf('%.2S', '🐍🐍'))
370      call assert_equal('', printf('%.1S', '🐍🐍'))
371
372      call assert_equal('[    あいう]', printf('[%10.6S]', 'あいうえお'))
373      call assert_equal('[  あいうえ]', printf('[%10.8S]', 'あいうえお'))
374      call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお'))
375      call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお'))
376
377      call assert_equal('あいう', printf('%S', 'あいう'))
378      call assert_equal('あいう', printf('%#S', 'あいう'))
379
380      call assert_equal('あb', printf('%2S', 'あb'))
381      call assert_equal('あb', printf('%.4S', 'あb'))
382      call assert_equal('あ', printf('%.2S', 'あb'))
383      call assert_equal(' あb', printf('%4S', 'あb'))
384      call assert_equal('0あb', printf('%04S', 'あb'))
385      call assert_equal('あb ', printf('%-4S', 'あb'))
386      call assert_equal('あ  ', printf('%-4.2S', 'あb'))
387
388      call assert_equal('aい', printf('%2S', 'aい'))
389      call assert_equal('aい', printf('%.4S', 'aい'))
390      call assert_equal('a', printf('%.2S', 'aい'))
391      call assert_equal(' aい', printf('%4S', 'aい'))
392      call assert_equal('0aい', printf('%04S', 'aい'))
393      call assert_equal('aい ', printf('%-4S', 'aい'))
394      call assert_equal('a   ', printf('%-4.2S', 'aい'))
395
396      call assert_equal('[あいう]', printf('[%05S]', 'あいう'))
397      call assert_equal('[あいう]', printf('[%06S]', 'あいう'))
398      call assert_equal('[0あいう]', printf('[%07S]', 'あいう'))
399
400      call assert_equal('[あiう]', printf('[%05S]', 'あiう'))
401      call assert_equal('[0あiう]', printf('[%06S]', 'あiう'))
402      call assert_equal('[00あiう]', printf('[%07S]', 'あiう'))
403
404      call assert_equal('[0あい]', printf('[%05.4S]', 'あいう'))
405      call assert_equal('[00あい]', printf('[%06.4S]', 'あいう'))
406      call assert_equal('[000あい]', printf('[%07.4S]', 'あいう'))
407
408      call assert_equal('[00あi]', printf('[%05.4S]', 'あiう'))
409      call assert_equal('[000あi]', printf('[%06.4S]', 'あiう'))
410      call assert_equal('[0000あi]', printf('[%07.4S]', 'あiう'))
411
412      call assert_equal('[0あい]', printf('[%05.5S]', 'あいう'))
413      call assert_equal('[00あい]', printf('[%06.5S]', 'あいう'))
414      call assert_equal('[000あい]', printf('[%07.5S]', 'あいう'))
415
416      call assert_equal('[あiう]', printf('[%05.5S]', 'あiう'))
417      call assert_equal('[0あiう]', printf('[%06.5S]', 'あiう'))
418      call assert_equal('[00あiう]', printf('[%07.5S]', 'あiう'))
419
420      call assert_equal('[0000000000]', printf('[%010.0S]', 'あいう'))
421      call assert_equal('[0000000000]', printf('[%010.1S]', 'あいう'))
422      call assert_equal('[00000000あ]', printf('[%010.2S]', 'あいう'))
423      call assert_equal('[00000000あ]', printf('[%010.3S]', 'あいう'))
424      call assert_equal('[000000あい]', printf('[%010.4S]', 'あいう'))
425      call assert_equal('[000000あい]', printf('[%010.5S]', 'あいう'))
426      call assert_equal('[0000あいう]', printf('[%010.6S]', 'あいう'))
427      call assert_equal('[0000あいう]', printf('[%010.7S]', 'あいう'))
428
429      call assert_equal('[0000000000]', printf('[%010.1S]', 'あiう'))
430      call assert_equal('[00000000あ]', printf('[%010.2S]', 'あiう'))
431      call assert_equal('[0000000あi]', printf('[%010.3S]', 'あiう'))
432      call assert_equal('[0000000あi]', printf('[%010.4S]', 'あiう'))
433      call assert_equal('[00000あiう]', printf('[%010.5S]', 'あiう'))
434      call assert_equal('[00000あiう]', printf('[%010.6S]', 'あiう'))
435      call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう'))
436
437      call assert_equal('1%', printf('%d%%', 1))
438      call assert_notequal('', printf('%p', "abc"))
439  END
440  call CheckLegacyAndVim9Success(lines)
441
442  call CheckLegacyAndVim9Failure(["call printf('123', 3)"], "E767:")
443
444  " this was using uninitialized memory
445  call CheckLegacyAndVim9Failure(["eval ''->printf()"], "E119:")
446endfunc
447
448func Test_printf_float()
449  if has('float')
450    let lines =<< trim END
451        call assert_equal('1.000000', printf('%f', 1))
452        call assert_equal('1.230000', printf('%f', 1.23))
453        call assert_equal('1.230000', printf('%F', 1.23))
454        call assert_equal('9999999.9', printf('%g', 9999999.9))
455        call assert_equal('9999999.9', printf('%G', 9999999.9))
456        call assert_equal('1.00000001e7', printf('%.8g', 10000000.1))
457        call assert_equal('1.00000001E7', printf('%.8G', 10000000.1))
458        call assert_equal('1.230000e+00', printf('%e', 1.23))
459        call assert_equal('1.230000E+00', printf('%E', 1.23))
460        call assert_equal('1.200000e-02', printf('%e', 0.012))
461        call assert_equal('-1.200000e-02', printf('%e', -0.012))
462        call assert_equal('0.33', printf('%.2f', 1.0 / 3.0))
463        call assert_equal('  0.33', printf('%6.2f', 1.0 / 3.0))
464        call assert_equal(' -0.33', printf('%6.2f', -1.0 / 3.0))
465        call assert_equal('000.33', printf('%06.2f', 1.0 / 3.0))
466        call assert_equal('-00.33', printf('%06.2f', -1.0 / 3.0))
467        call assert_equal('-00.33', printf('%+06.2f', -1.0 / 3.0))
468        call assert_equal('+00.33', printf('%+06.2f', 1.0 / 3.0))
469        call assert_equal(' 00.33', printf('% 06.2f', 1.0 / 3.0))
470        call assert_equal('000.33', printf('%06.2g', 1.0 / 3.0))
471        call assert_equal('-00.33', printf('%06.2g', -1.0 / 3.0))
472        call assert_equal('0.33', printf('%3.2f', 1.0 / 3.0))
473        call assert_equal('003.33e-01', printf('%010.2e', 1.0 / 3.0))
474        call assert_equal(' 03.33e-01', printf('% 010.2e', 1.0 / 3.0))
475        call assert_equal('+03.33e-01', printf('%+010.2e', 1.0 / 3.0))
476        call assert_equal('-03.33e-01', printf('%010.2e', -1.0 / 3.0))
477
478        #" When precision is 0, the dot should be omitted.
479        call assert_equal('  2', printf('%3.f', 7.0 / 3.0))
480        call assert_equal('  2', printf('%3.g', 7.0 / 3.0))
481        call assert_equal('  2e+00', printf('%7.e', 7.0 / 3.0))
482
483        #" Float zero can be signed.
484        call assert_equal('+0.000000', printf('%+f', 0.0))
485        call assert_equal('0.000000', printf('%f', 1.0 / (1.0 / 0.0)))
486        call assert_equal('-0.000000', printf('%f', 1.0 / (-1.0 / 0.0)))
487        call assert_equal('0.0', printf('%s', 1.0 / (1.0 / 0.0)))
488        call assert_equal('-0.0', printf('%s', 1.0 / (-1.0 / 0.0)))
489        call assert_equal('0.0', printf('%S', 1.0 / (1.0 / 0.0)))
490        call assert_equal('-0.0', printf('%S', 1.0 / (-1.0 / 0.0)))
491
492        #" Float infinity can be signed.
493        call assert_equal('inf', printf('%f', 1.0 / 0.0))
494        call assert_equal('-inf', printf('%f', -1.0 / 0.0))
495        call assert_equal('inf', printf('%g', 1.0 / 0.0))
496        call assert_equal('-inf', printf('%g', -1.0 / 0.0))
497        call assert_equal('inf', printf('%e', 1.0 / 0.0))
498        call assert_equal('-inf', printf('%e', -1.0 / 0.0))
499        call assert_equal('INF', printf('%F', 1.0 / 0.0))
500        call assert_equal('-INF', printf('%F', -1.0 / 0.0))
501        call assert_equal('INF', printf('%E', 1.0 / 0.0))
502        call assert_equal('-INF', printf('%E', -1.0 / 0.0))
503        call assert_equal('INF', printf('%E', 1.0 / 0.0))
504        call assert_equal('-INF', printf('%G', -1.0 / 0.0))
505        call assert_equal('+inf', printf('%+f', 1.0 / 0.0))
506        call assert_equal('-inf', printf('%+f', -1.0 / 0.0))
507        call assert_equal(' inf', printf('% f',  1.0 / 0.0))
508        call assert_equal('   inf', printf('%6f', 1.0 / 0.0))
509        call assert_equal('  -inf', printf('%6f', -1.0 / 0.0))
510        call assert_equal('   inf', printf('%6g', 1.0 / 0.0))
511        call assert_equal('  -inf', printf('%6g', -1.0 / 0.0))
512        call assert_equal('  +inf', printf('%+6f', 1.0 / 0.0))
513        call assert_equal('   inf', printf('% 6f', 1.0 / 0.0))
514        call assert_equal('  +inf', printf('%+06f', 1.0 / 0.0))
515        call assert_equal('inf   ', printf('%-6f', 1.0 / 0.0))
516        call assert_equal('-inf  ', printf('%-6f', -1.0 / 0.0))
517        call assert_equal('+inf  ', printf('%-+6f', 1.0 / 0.0))
518        call assert_equal(' inf  ', printf('%- 6f', 1.0 / 0.0))
519        call assert_equal('-INF  ', printf('%-6F', -1.0 / 0.0))
520        call assert_equal('+INF  ', printf('%-+6F', 1.0 / 0.0))
521        call assert_equal(' INF  ', printf('%- 6F', 1.0 / 0.0))
522        call assert_equal('INF   ', printf('%-6G', 1.0 / 0.0))
523        call assert_equal('-INF  ', printf('%-6G', -1.0 / 0.0))
524        call assert_equal('INF   ', printf('%-6E', 1.0 / 0.0))
525        call assert_equal('-INF  ', printf('%-6E', -1.0 / 0.0))
526        call assert_equal("str2float('inf')", printf('%s', 1.0 / 0.0))
527        call assert_equal("-str2float('inf')", printf('%s', -1.0 / 0.0))
528
529        #" Test special case where max precision is truncated at 340.
530        call assert_equal('1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.330f', 1.0))
531        call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.340f', 1.0))
532        call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.350f', 1.0))
533
534        #" Float nan (not a number) has no sign.
535        call assert_equal('nan', printf('%f', sqrt(-1.0)))
536        call assert_equal('nan', printf('%f', 0.0 / 0.0))
537        call assert_equal('nan', printf('%f', -0.0 / 0.0))
538        call assert_equal('nan', printf('%g', 0.0 / 0.0))
539        call assert_equal('nan', printf('%e', 0.0 / 0.0))
540        call assert_equal('NAN', printf('%F', 0.0 / 0.0))
541        call assert_equal('NAN', printf('%G', 0.0 / 0.0))
542        call assert_equal('NAN', printf('%E', 0.0 / 0.0))
543        call assert_equal('NAN', printf('%F', -0.0 / 0.0))
544        call assert_equal('NAN', printf('%G', -0.0 / 0.0))
545        call assert_equal('NAN', printf('%E', -0.0 / 0.0))
546        call assert_equal('   nan', printf('%6f', 0.0 / 0.0))
547        call assert_equal('   nan', printf('%06f', 0.0 / 0.0))
548        call assert_equal('nan   ', printf('%-6f', 0.0 / 0.0))
549        call assert_equal('nan   ', printf('%- 6f', 0.0 / 0.0))
550        call assert_equal("str2float('nan')", printf('%s', 0.0 / 0.0))
551        call assert_equal("str2float('nan')", printf('%s', -0.0 / 0.0))
552        call assert_equal("str2float('nan')", printf('%S', 0.0 / 0.0))
553        call assert_equal("str2float('nan')", printf('%S', -0.0 / 0.0))
554    END
555    call CheckLegacyAndVim9Success(lines)
556
557    call CheckLegacyAndVim9Failure(['echo printf("%f", "a")'], 'E807:')
558  endif
559endfunc
560
561func Test_printf_errors()
562  call CheckLegacyAndVim9Failure(['echo printf("%d", {})'], 'E728:')
563  call CheckLegacyAndVim9Failure(['echo printf("%d", [])'], 'E745:')
564  call CheckLegacyAndVim9Failure(['echo printf("%d", 1, 2)'], 'E767:')
565  call CheckLegacyAndVim9Failure(['echo printf("%*d", 1)'], 'E766:')
566  call CheckLegacyAndVim9Failure(['echo printf("%s")'], 'E766:')
567  if has('float')
568    call CheckLegacyAndVim9Failure(['echo printf("%d", 1.2)'], 'E805:')
569    call CheckLegacyAndVim9Failure(['echo printf("%f")'], 'E766:')
570  endif
571endfunc
572
573func Test_printf_64bit()
574  let lines =<< trim END
575      call assert_equal("123456789012345", printf('%d', 123456789012345))
576  END
577  call CheckLegacyAndVim9Success(lines)
578endfunc
579
580func Test_printf_spec_s()
581  let lines =<< trim END
582      #" number
583      call assert_equal("1234567890", printf('%s', 1234567890))
584
585      #" string
586      call assert_equal("abcdefgi", printf('%s', "abcdefgi"))
587
588      #" float
589      if has('float')
590        call assert_equal("1.23", printf('%s', 1.23))
591      endif
592
593      #" list
594      VAR lvalue = [1, 'two', ['three', 4]]
595      call assert_equal(string(lvalue), printf('%s', lvalue))
596
597      #" dict
598      VAR dvalue = {'key1': 'value1', 'key2': ['list', 'lvalue'], 'key3': {'dict': 'lvalue'}}
599      call assert_equal(string(dvalue), printf('%s', dvalue))
600
601      #" funcref
602      call assert_equal('printf', printf('%s', 'printf'->function()))
603
604      #" partial
605      call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s'])))
606  END
607  call CheckLegacyAndVim9Success(lines)
608endfunc
609
610func Test_printf_spec_b()
611  let lines =<< trim END
612      call assert_equal("0", printf('%b', 0))
613      call assert_equal("00001100", printf('%08b', 12))
614      call assert_equal("11111111", printf('%08b', 0xff))
615      call assert_equal("   1111011", printf('%10b', 123))
616      call assert_equal("0001111011", printf('%010b', 123))
617      call assert_equal(" 0b1111011", printf('%#10b', 123))
618      call assert_equal("0B01111011", printf('%#010B', 123))
619      call assert_equal("1001001100101100000001011010010", printf('%b', 1234567890))
620      call assert_equal("11100000100100010000110000011011101111101111001", printf('%b', 123456789012345))
621      call assert_equal("1111111111111111111111111111111111111111111111111111111111111111", printf('%b', -1))
622  END
623  call CheckLegacyAndVim9Success(lines)
624endfunc
625
626func Test_max_min_errors()
627  call CheckLegacyAndVim9Failure(['call max(v:true)'], ['E712:', 'E1013:', 'E1227:'])
628  call CheckLegacyAndVim9Failure(['call max(v:true)'], ['max()', 'E1013:', 'E1227:'])
629  call CheckLegacyAndVim9Failure(['call min(v:true)'], ['E712:', 'E1013:', 'E1227:'])
630  call CheckLegacyAndVim9Failure(['call min(v:true)'], ['min()', 'E1013:', 'E1227:'])
631endfunc
632
633func Test_function_with_funcref()
634  let lines =<< trim END
635      let s:F = function('type')
636      let s:Fref = function(s:F)
637      call assert_equal(v:t_string, s:Fref('x'))
638      call assert_fails("call function('s:F')", 'E700:')
639
640      call assert_fails("call function('foo()')", 'E475:')
641      call assert_fails("call function('foo()')", 'foo()')
642      call assert_fails("function('')", 'E129:')
643
644      let s:Len = {s -> strlen(s)}
645      call assert_equal(6, s:Len('foobar'))
646      let name = string(s:Len)
647      " can evaluate "function('<lambda>99')"
648      call execute('let Ref = ' .. name)
649      call assert_equal(4, Ref('text'))
650  END
651  call CheckScriptSuccess(lines)
652
653  let lines =<< trim END
654      vim9script
655      var F = function('type')
656      var Fref = function(F)
657      call assert_equal(v:t_string, Fref('x'))
658      call assert_fails("call function('F')", 'E700:')
659
660      call assert_fails("call function('foo()')", 'E475:')
661      call assert_fails("call function('foo()')", 'foo()')
662      call assert_fails("function('')", 'E129:')
663
664      var Len = (s) => strlen(s)
665      call assert_equal(6, Len('foobar'))
666      var name = string(Len)
667      # can evaluate "function('<lambda>99')"
668      call execute('var Ref = ' .. name)
669      call assert_equal(4, Ref('text'))
670  END
671  call CheckScriptSuccess(lines)
672endfunc
673
674func Test_funcref()
675  func! One()
676    return 1
677  endfunc
678  let OneByName = function('One')
679  let OneByRef = funcref('One')
680  func! One()
681    return 2
682  endfunc
683  call assert_equal(2, OneByName())
684  call assert_equal(1, OneByRef())
685  let OneByRef = 'One'->funcref()
686  call assert_equal(2, OneByRef())
687  call assert_fails('echo funcref("{")', 'E475:')
688  let OneByRef = funcref("One", repeat(["foo"], 20))
689  call assert_fails('let OneByRef = funcref("One", repeat(["foo"], 21))', 'E118:')
690  call assert_fails('echo function("min") =~ function("min")', 'E694:')
691endfunc
692
693" Test for calling function() and funcref() outside of a Vim script context.
694func Test_function_outside_script()
695  let cleanup =<< trim END
696    call writefile([execute('messages')], 'Xtest.out')
697    qall
698  END
699  call writefile(cleanup, 'Xverify.vim')
700  call RunVim([], [], "-c \"echo function('s:abc')\" -S Xverify.vim")
701  call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0])
702  call RunVim([], [], "-c \"echo funcref('s:abc')\" -S Xverify.vim")
703  call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0])
704  call delete('Xtest.out')
705  call delete('Xverify.vim')
706endfunc
707
708func Test_setmatches()
709  let lines =<< trim END
710      hi def link 1 Comment
711      hi def link 2 PreProc
712      VAR set = [{"group": 1, "pattern": 2, "id": 3, "priority": 4}]
713      VAR exp = [{"group": '1', "pattern": '2', "id": 3, "priority": 4}]
714      if has('conceal')
715        LET set[0]['conceal'] = 5
716        LET exp[0]['conceal'] = '5'
717      endif
718      eval set->setmatches()
719      call assert_equal(exp, getmatches())
720  END
721  call CheckLegacyAndVim9Success(lines)
722
723  call CheckLegacyAndVim9Failure(['VAR m = setmatches([], [])'], ['E745:', 'E1013:', 'E1210:'])
724endfunc
725
726func Test_empty_concatenate()
727  let lines =<< trim END
728      call assert_equal('b', 'a'[4 : 0] .. 'b')
729      call assert_equal('b', 'b' .. 'a'[4 : 0])
730  END
731  call CheckLegacyAndVim9Success(lines)
732endfunc
733
734func Test_broken_number()
735  call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 1X'], 'E15:')
736  call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0b1X'], 'E15:')
737  call CheckLegacyAndVim9Failure(['echo 0b12'], 'E15:')
738  call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0x1X'], 'E15:')
739  call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 011X'], 'E15:')
740
741  call CheckLegacyAndVim9Success(['call assert_equal(2, str2nr("2a"))'])
742
743  call CheckLegacyAndVim9Failure(['inoremap <Char-0b1z> b'], 'E474:')
744endfunc
745
746func Test_eval_after_if()
747  let s:val = ''
748  func SetVal(x)
749    let s:val ..= a:x
750  endfunc
751  if 0 | eval SetVal('a') | endif | call SetVal('b')
752  call assert_equal('b', s:val)
753endfunc
754
755func Test_divide_by_zero()
756  " only tests that this doesn't crash, the result is not important
757  echo 0 / 0
758  echo 0 / 0 / -1
759endfunc
760
761" Test for command-line completion of expressions
762func Test_expr_completion()
763  CheckFeature cmdline_compl
764  for cmd in [
765	\ 'let a = ',
766	\ 'const a = ',
767	\ 'if',
768	\ 'elseif',
769	\ 'while',
770	\ 'for',
771	\ 'echo',
772	\ 'echon',
773	\ 'execute',
774	\ 'echomsg',
775	\ 'echoerr',
776	\ 'call',
777	\ 'return',
778	\ 'cexpr',
779	\ 'caddexpr',
780	\ 'cgetexpr',
781	\ 'lexpr',
782	\ 'laddexpr',
783	\ 'lgetexpr']
784    call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
785    call assert_equal('"' . cmd . ' getline(', getreg(':'))
786  endfor
787
788  " completion for the expression register
789  call feedkeys(":\"\<C-R>=float2\t\"\<C-B>\"\<CR>", 'xt')
790  call assert_equal('"float2nr("', @=)
791
792  " completion for window local variables
793  let w:wvar1 = 10
794  let w:wvar2 = 10
795  call feedkeys(":echo w:wvar\<C-A>\<C-B>\"\<CR>", 'xt')
796  call assert_equal('"echo w:wvar1 w:wvar2', @:)
797  unlet w:wvar1 w:wvar2
798
799  " completion for tab local variables
800  let t:tvar1 = 10
801  let t:tvar2 = 10
802  call feedkeys(":echo t:tvar\<C-A>\<C-B>\"\<CR>", 'xt')
803  call assert_equal('"echo t:tvar1 t:tvar2', @:)
804  unlet t:tvar1 t:tvar2
805
806  " completion for variables
807  let g:tvar1 = 1
808  let g:tvar2 = 2
809  call feedkeys(":let g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
810  call assert_equal('"let g:tvar1 g:tvar2', @:)
811  " completion for variables after a ||
812  call feedkeys(":echo 1 || g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
813  call assert_equal('"echo 1 || g:tvar1 g:tvar2', @:)
814
815  " completion for options
816  "call feedkeys(":echo &compat\<C-A>\<C-B>\"\<CR>", 'xt')
817  "call assert_equal('"echo &compatible', @:)
818  "call feedkeys(":echo 1 && &compat\<C-A>\<C-B>\"\<CR>", 'xt')
819  "call assert_equal('"echo 1 && &compatible', @:)
820  call feedkeys(":echo &g:equala\<C-A>\<C-B>\"\<CR>", 'xt')
821  call assert_equal('"echo &g:equalalways', @:)
822
823  " completion for string
824  call feedkeys(":echo \"Hello\\ World\"\<C-A>\<C-B>\"\<CR>", 'xt')
825  call assert_equal("\"echo \"Hello\\ World\"\<C-A>", @:)
826  call feedkeys(":echo 'Hello World'\<C-A>\<C-B>\"\<CR>", 'xt')
827  call assert_equal("\"echo 'Hello World'\<C-A>", @:)
828
829  " completion for command after a |
830  call feedkeys(":echo 'Hello' | cwin\<C-A>\<C-B>\"\<CR>", 'xt')
831  call assert_equal("\"echo 'Hello' | cwindow", @:)
832
833  " completion for environment variable
834  let $X_VIM_TEST_COMPLETE_ENV = 'foo'
835  call feedkeys(":let $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
836  call assert_match('"let $X_VIM_TEST_COMPLETE_ENV', @:)
837  unlet $X_VIM_TEST_COMPLETE_ENV
838endfunc
839
840" Test for errors in expression evaluation
841func Test_expr_eval_error()
842  call CheckLegacyAndVim9Failure(["VAR i = 'abc' .. []"], ['E730:', 'E1105:', 'E730:'])
843  call CheckLegacyAndVim9Failure(["VAR l = [] + 10"], ['E745:', 'E1051:', 'E745:'])
844  call CheckLegacyAndVim9Failure(["VAR v = 10 + []"], ['E745:', 'E1051:', 'E745:'])
845  call CheckLegacyAndVim9Failure(["VAR v = 10 / []"], ['E745:', 'E1036:', 'E745:'])
846  call CheckLegacyAndVim9Failure(["VAR v = -{}"], ['E728:', 'E1012:', 'E728:'])
847endfunc
848
849func Test_white_in_function_call()
850  let lines =<< trim END
851      VAR text = substitute ( 'some text' , 't' , 'T' , 'g' )
852      call assert_equal('some TexT', text)
853  END
854  call CheckTransLegacySuccess(lines)
855
856  let lines =<< trim END
857      var text = substitute ( 'some text' , 't' , 'T' , 'g' )
858      call assert_equal('some TexT', text)
859  END
860  call CheckDefAndScriptFailure(lines, ['E1001:', 'E121:'])
861endfunc
862
863" Test for float value comparison
864func Test_float_compare()
865  CheckFeature float
866
867  let lines =<< trim END
868      call assert_true(1.2 == 1.2)
869      call assert_true(1.0 != 1.2)
870      call assert_true(1.2 > 1.0)
871      call assert_true(1.2 >= 1.2)
872      call assert_true(1.0 < 1.2)
873      call assert_true(1.2 <= 1.2)
874      call assert_true(+0.0 == -0.0)
875      #" two NaNs (not a number) are not equal
876      call assert_true(sqrt(-4.01) != (0.0 / 0.0))
877      #" two inf (infinity) are equal
878      call assert_true((1.0 / 0) == (2.0 / 0))
879      #" two -inf (infinity) are equal
880      call assert_true(-(1.0 / 0) == -(2.0 / 0))
881      #" +infinity != -infinity
882      call assert_true((1.0 / 0) != -(2.0 / 0))
883  END
884  call CheckLegacyAndVim9Success(lines)
885endfunc
886
887func Test_string_interp()
888  let lines =<< trim END
889    call assert_equal('', $"")
890    call assert_equal('foobar', $"foobar")
891    #" Escaping rules.
892    call assert_equal('"foo"{bar}', $"\"foo\"{{bar}}")
893    call assert_equal('"foo"{bar}', $'"foo"{{bar}}')
894    call assert_equal('foobar', $"{"foo"}" .. $'{'bar'}')
895    #" Whitespace before/after the expression.
896    call assert_equal('3', $"{ 1 + 2 }")
897    #" String conversion.
898    call assert_equal('hello from ' .. v:version, $"hello from {v:version}")
899    call assert_equal('hello from ' .. v:version, $'hello from {v:version}')
900    #" Paper over a small difference between Vim script behaviour.
901    call assert_equal(string(v:true), $"{v:true}")
902    call assert_equal('(1+1=2)', $"(1+1={1 + 1})")
903    #" Hex-escaped opening brace: char2nr('{') == 0x7b
904    call assert_equal('esc123ape', $"esc{123}ape")
905    call assert_equal('me{}me', $"me{"\x7b"}\x7dme")
906    VAR var1 = "sun"
907    VAR var2 = "shine"
908    call assert_equal('sunshine', $"{var1}{var2}")
909    call assert_equal('sunsunsun', $"{var1->repeat(3)}")
910    #" Multibyte strings.
911    call assert_equal('say ハロー・ワールド', $"say {'ハロー・ワールド'}")
912    #" Nested.
913    call assert_equal('foobarbaz', $"foo{$"{'bar'}"}baz")
914    #" Do not evaluate blocks when the expr is skipped.
915    VAR tmp = 0
916    if v:false
917      echo "${ LET tmp += 1 }"
918    endif
919    call assert_equal(0, tmp)
920
921    #" Dict interpolation
922    VAR d = {'a': 10, 'b': [1, 2]}
923    call assert_equal("{'a': 10, 'b': [1, 2]}", $'{d}')
924    VAR emptydict = {}
925    call assert_equal("a{}b", $'a{emptydict}b')
926    VAR nulldict = v:_null_dict
927    call assert_equal("a{}b", $'a{nulldict}b')
928
929    #" List interpolation
930    VAR l = ['a', 'b', 'c']
931    call assert_equal("['a', 'b', 'c']", $'{l}')
932    VAR emptylist = []
933    call assert_equal("a[]b", $'a{emptylist}b')
934    VAR nulllist = v:_null_list
935    call assert_equal("a[]b", $'a{nulllist}b')
936
937    #" Stray closing brace.
938    call assert_fails('echo $"moo}"', 'E1278:')
939    #" Undefined variable in expansion.
940    call assert_fails('echo $"{moo}"', 'E121:')
941    #" Empty blocks are rejected.
942    call assert_fails('echo $"{}"', 'E15:')
943    call assert_fails('echo $"{   }"', 'E15:')
944  END
945  call CheckLegacyAndVim9Success(lines)
946
947  let lines =<< trim END
948    call assert_equal('5', $"{({x -> x + 1})(4)}")
949  END
950  call CheckLegacySuccess(lines)
951
952  let lines =<< trim END
953    call assert_equal('5', $"{((x) => x + 1)(4)}")
954    call assert_fails('echo $"{ # foo }"', 'E1279:')
955  END
956  call CheckDefAndScriptSuccess(lines)
957endfunc
958
959" vim: shiftwidth=2 sts=2 expandtab