" Tests for expressions.
source check.vim
import './vim9.vim' as v9
func Test_equal()
let base = {}
func base.method()
return 1
endfunc
func base.other() dict
return 1
endfunc
let instance = copy(base)
call assert_true(base.method == instance.method)
call assert_true([base.method] == [instance.method])
call assert_true(base.other == instance.other)
call assert_true([base.other] == [instance.other])
call assert_false(base.method == base.other)
call assert_false([base.method] == [base.other])
call assert_false(base.method == instance.other)
call assert_false([base.method] == [instance.other])
call assert_fails('echo base.method > instance.method')
call assert_equal(0, test_null_function() == function('min'))
call assert_equal(1, test_null_function() == test_null_function())
call assert_fails('eval 10 == test_unknown()', 'E685:')
endfunc
func Test_version()
call assert_true(has('patch-7.4.001'))
call assert_true(has('patch-7.4.01'))
call assert_true(has('patch-7.4.1'))
call assert_true(has('patch-6.9.999'))
call assert_true(has('patch-7.1.999'))
call assert_true(has('patch-7.4.123'))
call assert_true(has('patch-7.4.123 ')) " Traling space can be allowed.
call assert_false(has('patch-7'))
call assert_false(has('patch-7.4'))
call assert_false(has('patch-7.4.'))
call assert_false(has('patch-9.1.0'))
call assert_false(has('patch-9.9.1'))
call assert_false(has('patch-abc'))
call assert_false(has('patchabc'))
call assert_false(has('patch-8x001'))
call assert_false(has('patch-9X0X0'))
call assert_false(has('patch-9-0-0'))
call assert_false(has('patch-09.0.0'))
call assert_false(has('patch-9.00.0'))
endfunc
func Test_op_ternary()
let lines =<< trim END
call assert_equal('yes', 1 ? 'yes' : 'no')
call assert_equal('no', 0 ? 'yes' : 'no')
call assert_fails('echo [1] ? "yes" : "no"', 'E745:')
call assert_fails('echo {} ? "yes" : "no"', 'E728:')
END
call v9.CheckLegacyAndVim9Success(lines)
call assert_equal('no', 'x' ? 'yes' : 'no')
call v9.CheckDefAndScriptFailure(["'x' ? 'yes' : 'no'"], 'E1135:')
call assert_equal('yes', '1x' ? 'yes' : 'no')
call v9.CheckDefAndScriptFailure(["'1x' ? 'yes' : 'no'"], 'E1135:')
endfunc
func Test_op_falsy()
let lines =<< trim END
call assert_equal(v:true, v:true ?? 456)
call assert_equal(123, 123 ?? 456)
call assert_equal('yes', 'yes' ?? 456)
call assert_equal(0z00, 0z00 ?? 456)
call assert_equal([1], [1] ?? 456)
call assert_equal({'one': 1}, {'one': 1} ?? 456)
call assert_equal(0.1, 0.1 ?? 456)
call assert_equal(456, v:false ?? 456)
call assert_equal(456, 0 ?? 456)
call assert_equal(456, '' ?? 456)
call assert_equal(456, 0z ?? 456)
call assert_equal(456, [] ?? 456)
call assert_equal(456, {} ?? 456)
call assert_equal(456, 0.0 ?? 456)
END
call v9.CheckLegacyAndVim9Success(lines)
endfunc
func Test_dict()
let lines =<< trim END
VAR d = {'': 'empty', 'a': 'a', 0: 'zero'}
call assert_equal('empty', d[''])
call assert_equal('a', d['a'])
call assert_equal('zero', d[0])
call assert_true(has_key(d, ''))
call assert_true(has_key(d, 'a'))
LET d[''] = 'none'
LET d['a'] = 'aaa'
call assert_equal('none', d[''])
call assert_equal('aaa', d['a'])
LET d[ 'b' ] = 'bbb'
call assert_equal('bbb', d[ 'b' ])
END
call v9.CheckLegacyAndVim9Success(lines)
call v9.CheckLegacyAndVim9Failure(["VAR i = has_key([], 'a')"], ['E1206:', 'E1013:', 'E1206:'])
endfunc
func Test_strgetchar()
let lines =<< trim END
call assert_equal(char2nr('a'), strgetchar('axb', 0))
call assert_equal(char2nr('x'), 'axb'->strgetchar(1))
call assert_equal(char2nr('b'), strgetchar('axb', 2))
call assert_equal(-1, strgetchar('axb', -1))
call assert_equal(-1, strgetchar('axb', 3))
call assert_equal(-1, strgetchar('', 0))
END
call v9.CheckLegacyAndVim9Success(lines)
call v9.CheckLegacyAndVim9Failure(["VAR c = strgetchar([], 1)"], ['E730:', 'E1013:', 'E1174:'])
call v9.CheckLegacyAndVim9Failure(["VAR c = strgetchar('axb', [])"], ['E745:', 'E1013:', 'E1210:'])
endfunc
func Test_strcharpart()
let lines =<< trim END
call assert_equal('a', strcharpart('axb', 0, 1))
call assert_equal('x', 'axb'->strcharpart(1, 1))
call assert_equal('b', strcharpart('axb', 2, 1))
call assert_equal('xb', strcharpart('axb', 1))
call assert_equal('', strcharpart('axb', 1, 0))
call assert_equal('', strcharpart('axb', 1, -1))
call assert_equal('', strcharpart('axb', -1, 1))
call assert_equal('', strcharpart('axb', -2, 2))
call assert_equal('a', strcharpart('axb', -1, 2))
call assert_equal('edit', "editor"[-10 : 3])
END
call v9.CheckLegacyAndVim9Success(lines)
endfunc
func Test_getreg_empty_list()
let lines =<< trim END
call assert_equal('', getreg('x'))
call assert_equal([], getreg('x', 1, 1))
VAR x = getreg('x', 1, 1)
VAR y = x
call add(x, 'foo')
call assert_equal(['foo'], y)