" 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()', ['E340:', '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 ')) " Trailing space can be allowed.
call assert_true(has('patch-9.1.0'))
call assert_true(has('patch-9.1.0000'))
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.2.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)
call assert_equal(456, v:null ?? 456)
call assert_equal(456, v:none ?? 456)
call assert_equal(456, test_null_string() ?? 456)
call assert_equal(456, test_null_blob() ?? 456)
call assert_equal(456, test_null_list() ?? 456)
call assert_equal(456, test_null_dict() ?? 456)
call assert_equal(456, test_null_function() ?? 456)
call assert_equal(456, test_null_partial() ?? 456)
if has('job')
call assert_equal(456, test_null_job() ?? 456)
endif
if has('channel')
call assert_equal(456, test_null_channel() ?? 456)
endif
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(''