robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Tests for getbufvar(), getwinvar(), gettabvar() and gettabwinvar(). 2 3local n =require ( 'test.functional.testnvim' )() 4local t =require ( 'test.testutil' ) 5 6local describe , it , setup =t . describe , t . it , t . setup 7local insert , source =n . insert , n . source 8local clear , expect =n . clear , n . expect 9 10describe ( 'context variables' , function () 11setup ( clear ) 12 13it ( 'is working' , function () 14insert ( 'start:' ) 15 16-- Test for getbufvar(). 17-- Use strings to test for memory leaks. 18source ( [[ 19function Getbufscope(buf, ...) 20let ret = call('getbufvar', [a:buf, ''] + a:000) 21if type(ret) == type({}) 22return filter(copy(ret), 'v:key isnot# "changedtick"') 23else 24return ret 25endif 26endfunction 27let t:testvar='abcd' 28$put =string(gettabvar(1, 'testvar')) 29$put =string(gettabvar(1, 'testvar')) 30let b:var_num = '1234' 31let def_num = '5678' 32$put =string(getbufvar(1, 'var_num')) 33$put =string(getbufvar(1, 'var_num', def_num)) 34$put =string(Getbufscope(1)) 35$put =string(Getbufscope(1, def_num)) 36unlet b:var_num 37$put =string(getbufvar(1, 'var_num', def_num)) 38$put =string(Getbufscope(1)) 39$put =string(Getbufscope(1, def_num)) 40$put =string(Getbufscope(9)) 41$put =string(Getbufscope(9, def_num)) 42unlet def_num 43$put =string(getbufvar(1, '&autoindent')) 44$put =string(getbufvar(1, '&autoindent', 1)) 45]] ) 46 47-- Open new window with forced option values. 48source ( [[ 49set fileformats=unix,dos 50new ++ff=dos ++bin ++enc=iso-8859-2 51let otherff = getbufvar(bufnr('%'), '&fileformat') 52let otherbin = getbufvar(bufnr('%'), '&bin') 53let otherfenc = getbufvar(bufnr('%'), '&fenc') 54close 55$put =otherff 56$put =string(otherbin) 57$put =otherfenc 58unlet otherff otherbin otherfenc 59]] ) 60 61-- Test for getwinvar(). 62source ( [[ 63let w:var_str = "Dance" 64let def_str = "Chance" 65$put =string(getwinvar(1, 'var_str')) 66$put =string(getwinvar(1, 'var_str', def_str)) 67$put =string(getwinvar(1, '')) 68$put =string(getwinvar(1, '', def_str)) 69unlet w:var_str 70$put =string(getwinvar(1, 'var_str', def_str)) 71$put =string(getwinvar(1, '')) 72$put =string(getwinvar(1, '', def_str)) 73$put =string(getwinvar(9, '')) 74$put =string(getwinvar(9, '', def_str)) 75$put =string(getwinvar(1, '&nu')) 76$put =string(getwinvar(1, '&nu', 1)) 77unlet def_str 78]] ) 79 80-- Test for gettabvar(). 81source ( [[ 82tabnew 83tabnew 84let t:var_list = [1, 2, 3] 85let t:other = 777 86let def_list = [4, 5, 6, 7] 87tabrewind 88$put =string(gettabvar(3, 'var_list')) 89$put =string(gettabvar(3, 'var_list', def_list)) 90$put =string(gettabvar(3, '')) 91$put =string(gettabvar(3, '', def_list)) 92tablast 93unlet t:var_list 94tabrewind 95$put =string(gettabvar(3, 'var_list', def_list)) 96$put =string(gettabvar(9, '')) 97$put =string(gettabvar(9, '', def_list)) 98$put =string(gettabvar(3, '&nu')) 99$put =string(gettabvar(3, '&nu', def_list)) 100unlet def_list 101tabonly 102]] ) 103 104-- Test for gettabwinvar(). 105source ( [[ 106tabnew 107tabnew 108tabprev 109split 110split 111wincmd w 112vert split 113wincmd w 114let w:var_dict = {'dict': 'tabwin'} 115let def_dict = {'dict2': 'newval'} 116wincmd b 117tabrewind 118$put =string(gettabwinvar(2, 3, 'var_dict')) 119$put =string(gettabwinvar(2, 3, 'var_dict', def_dict)) 120$put =string(gettabwinvar(2, 3, '')) 121$put =string(gettabwinvar(2, 3, '', def_dict)) 122tabnext 1233wincmd w 124unlet w:var_dict 125tabrewind 126$put =string(gettabwinvar(2, 3, 'var_dict', def_dict)) 127$put =string(gettabwinvar(2, 3, '')) 128$put =string(gettabwinvar(2, 3, '', def_dict)) 129$put =string(gettabwinvar(2, 9, '')) 130$put =string(gettabwinvar(2, 9, '', def_dict)) 131$put =string(gettabwinvar(9, 3, '')) 132$put =string(gettabwinvar(9, 3, '', def_dict)) 133unlet def_dict 134$put =string(gettabwinvar(2, 3, '&nux')) 135$put =string(gettabwinvar(2, 3, '&nux', 1)) 136tabonly 137]] ) 138 139-- Assert buffer contents. 140expect ( [[ 141start: 142'abcd' 143'abcd' 144'1234' 145'1234' 146{'var_num': '1234'} 147{'var_num': '1234'} 148'5678' 149{} 150{} 151'' 152'5678' 1530 1540 155dos 1561 157iso-8859-2 158'Dance' 159'Dance' 160{'var_str': 'Dance'} 161{'var_str': 'Dance'} 162'Chance' 163{} 164{} 165'' 166'Chance' 1670 1680 169[1, 2, 3] 170[1, 2, 3] 171{'var_list': [1, 2, 3], 'other': 777} 172{'var_list': [1, 2, 3], 'other': 777} 173[4, 5, 6, 7] 174'' 175[4, 5, 6, 7] 176'' 177[4, 5, 6, 7] 178{'dict': 'tabwin'} 179{'dict': 'tabwin'} 180{'var_dict': {'dict': 'tabwin'}} 181{'var_dict': {'dict': 'tabwin'}} 182{'dict2': 'newval'} 183{} 184{} 185'' 186{'dict2': 'newval'} 187'' 188{'dict2': 'newval'} 189'' 1901]] ) 191end ) 192end )