" Test Vim9 assignments
source check.vim
source vim9.vim
let s:appendToMe = 'xxx'
let s:addToMe = 111
let g:existing = 'yes'
let g:inc_counter = 1
let $SOME_ENV_VAR = 'some'
let g:alist = [7]
let g:astring = 'text'
def Test_assignment_bool()
var bool1: bool = true
assert_equal(v:true, bool1)
var bool2: bool = false
assert_equal(v:false, bool2)
var bool3: bool = 0
assert_equal(false, bool3)
var bool4: bool = 1
assert_equal(true, bool4)
var bool5: bool = 1 && true
assert_equal(true, bool5)
var bool6: bool = 0 && 1
assert_equal(false, bool6)
var bool7: bool = 0 || 1 && true
assert_equal(true, bool7)
var lines =<< trim END
vim9script
def GetFlag(): bool
var flag: bool = 1
return flag
enddef
var flag: bool = GetFlag()
assert_equal(true, flag)
flag = 0
assert_equal(false, flag)
flag = 1
assert_equal(true, flag)
flag = 1 || true
assert_equal(true, flag)
flag = 1 && false
assert_equal(false, flag)
END
CheckScriptSuccess(lines)
CheckDefAndScriptFailure(['var x: bool = 2'], 'E1012:')
CheckDefAndScriptFailure(['var x: bool = -1'], 'E1012:')
CheckDefAndScriptFailure(['var x: bool = [1]'], 'E1012:')
CheckDefAndScriptFailure(['var x: bool = {}'], 'E1012:')
CheckDefAndScriptFailure(['var x: bool = "x"'], 'E1012:')
enddef
def Test_syntax()
var name = 234
var other: list<string> = ['asdf']
enddef
def Test_assignment()
CheckDefFailure(['var x:string'], 'E1069:')
CheckDefFailure(['var x:string = "x"'], 'E1069:')
CheckDefFailure(['var a:string = "x"'], 'E1069:')
CheckDefFailure(['var lambda = {-> "lambda"}'], 'E704:')
CheckScriptFailure(['var x = "x"'], 'E1124:')
var nr: number = 1234
CheckDefFailure(['var nr: number = "asdf"'], 'E1012:')
var a: number = 6 #comment
assert_equal(6, a)
if has('channel')
var chan1: channel
assert_equal('fail', ch_status(chan1))
var job1: job
assert_equal('fail', job_status(job1))
# calling job_start() is in test_vim9_fails.vim, it causes leak reports
endif
if has('float')
var float1: float = 3.4
endif
var Funky1: func
var Funky2: func = function('len')
var Party2: func = funcref('g:Test_syntax')
g:newvar = 'new' #comment
assert_equal('new', g:newvar)
assert_equal('yes', g:existing)
g:existing = 'no'
assert_equal('no', g:existing)
v:char = 'abc'
assert_equal('abc', v:char)
$ENVVAR = 'foobar'
assert_equal('foobar', $ENVVAR)
$ENVVAR = ''
var lines =<< trim END
vim9script
$ENVVAR = 'barfoo'
assert_equal('barfoo', $ENVVAR)
$ENVVAR = ''
END
CheckScriptSuccess(lines)
s:appendToMe ..= 'yyy'
assert_equal('xxxyyy', s:appendToMe)
s:addToMe += 222
assert_equal(333, s:addToMe)
s:newVar = 'new'
assert_equal('new', s:newVar)
set ts=7
&ts += 1
assert_equal(8, &ts)
&ts -= 3
assert_equal(5, &ts)
&ts *= 2
assert_equal(10, &ts)
&ts /= 3
assert_equal(3, &ts)
set ts=10
&ts %= 4
assert_equal(2, &ts)
if has('float')
var f100: float = 100.0
f100 /= 5
assert_equal(20.0, f100)
var f200: float = 200.0
f200 /= 5.0
assert_equal(40.<