test_getvar.vim (6302B)
1 " Tests for getwinvar(), gettabvar(), gettabwinvar() and get(). 2 3 func Test_var() 4 " Use strings to test for memory leaks. First, check that in an empty 5 " window, gettabvar() returns the correct value 6 let t:testvar='abcd' 7 call assert_equal('abcd', gettabvar(1, 'testvar')) 8 call assert_equal('abcd', gettabvar(1, 'testvar')) 9 10 " test for getwinvar() 11 let w:var_str = "Dance" 12 let def_str = "Chance" 13 call assert_equal('Dance', getwinvar(1, 'var_str')) 14 call assert_equal('Dance', getwinvar(1, 'var_str', def_str)) 15 call assert_equal({'var_str': 'Dance'}, 1->getwinvar('')) 16 call assert_equal({'var_str': 'Dance'}, 1->getwinvar('', def_str)) 17 unlet w:var_str 18 call assert_equal('Chance', getwinvar(1, 'var_str', def_str)) 19 call assert_equal({}, getwinvar(1, '')) 20 call assert_equal({}, getwinvar(1, '', def_str)) 21 call assert_equal('', getwinvar(9, '')) 22 call assert_equal('Chance', getwinvar(9, '', def_str)) 23 call assert_equal(0, getwinvar(1, '&nu')) 24 call assert_equal(0, getwinvar(1, '&nu', 1)) 25 call assert_match(v:t_dict, type(getwinvar(1, '&'))) 26 call assert_match(v:t_dict, type(getwinvar(1, '&', def_str))) 27 call assert_equal('', getwinvar(9, '&')) 28 call assert_equal('Chance', getwinvar(9, '&', def_str)) 29 call assert_equal('', getwinvar(1, '&nux')) 30 call assert_equal('Chance', getwinvar(1, '&nux', def_str)) 31 unlet def_str 32 33 " test for gettabvar() 34 tabnew 35 tabnew 36 let t:var_list = [1, 2, 3] 37 let t:other = 777 38 let def_list = [4, 5, 6, 7] 39 tabrewind 40 call assert_equal([1, 2, 3], 3->gettabvar('var_list')) 41 call assert_equal([1, 2, 3], gettabvar(3, 'var_list', def_list)) 42 call assert_equal({'var_list': [1, 2, 3], 'other': 777}, gettabvar(3, '')) 43 call assert_equal({'var_list': [1, 2, 3], 'other': 777}, 44 \ gettabvar(3, '', def_list)) 45 46 tablast 47 unlet t:var_list 48 tabrewind 49 call assert_equal([4, 5, 6, 7], gettabvar(3, 'var_list', def_list)) 50 call assert_equal('', gettabvar(9, '')) 51 call assert_equal([4, 5, 6, 7], gettabvar(9, '', def_list)) 52 call assert_equal('', gettabvar(3, '&nu')) 53 call assert_equal([4, 5, 6, 7], gettabvar(3, '&nu', def_list)) 54 unlet def_list 55 tabonly 56 57 " test for gettabwinvar() 58 tabnew 59 tabnew 60 tabprev 61 split 62 split 63 wincmd w 64 vert split 65 wincmd w 66 let w:var_dict = {'dict': 'tabwin'} 67 let def_dict = {'dict2': 'newval'} 68 wincmd b 69 tabrewind 70 call assert_equal({'dict': 'tabwin'}, 2->gettabwinvar(3, 'var_dict')) 71 call assert_equal({'dict': 'tabwin'}, 72 \ gettabwinvar(2, 3, 'var_dict', def_dict)) 73 call assert_equal({'var_dict': {'dict': 'tabwin'}}, gettabwinvar(2, 3, '')) 74 call assert_equal({'var_dict': {'dict': 'tabwin'}}, 75 \ gettabwinvar(2, 3, '', def_dict)) 76 77 tabnext 78 3wincmd w 79 unlet w:var_dict 80 tabrewind 81 call assert_equal({'dict2': 'newval'}, 82 \ gettabwinvar(2, 3, 'var_dict', def_dict)) 83 call assert_equal({}, gettabwinvar(2, 3, '')) 84 call assert_equal({}, gettabwinvar(2, 3, '', def_dict)) 85 call assert_equal("", gettabwinvar(2, 9, '')) 86 call assert_equal({'dict2': 'newval'}, gettabwinvar(2, 9, '', def_dict)) 87 call assert_equal('', gettabwinvar(9, 3, '')) 88 call assert_equal({'dict2': 'newval'}, gettabwinvar(9, 3, '', def_dict)) 89 90 unlet def_dict 91 92 call assert_match(v:t_dict, type(gettabwinvar(2, 3, '&'))) 93 call assert_match(v:t_dict, type(gettabwinvar(2, 3, '&', 1))) 94 call assert_equal("", gettabwinvar(9, 2020, '')) 95 call assert_equal(1, gettabwinvar(9, 2020, '', 1)) 96 call assert_equal('', gettabwinvar(9, 2020, '&')) 97 call assert_equal(1, gettabwinvar(9, 2020, '&', 1)) 98 call assert_equal('', gettabwinvar(2, 3, '&nux')) 99 call assert_equal(1, gettabwinvar(2, 3, '&nux', 1)) 100 tabonly 101 endfunc 102 103 " It was discovered that "gettabvar()" would fail if called from within the 104 " tabline when the user closed a window. This test confirms the fix. 105 func Test_gettabvar_in_tabline() 106 let t:var_str = 'value' 107 108 set tabline=%{assert_equal('value',gettabvar(1,'var_str'))} 109 set showtabline=2 110 111 " Simulate the user opening a split (which becomes window #1) and then 112 " closing the split, which triggers the redrawing of the tabline. 113 leftabove split 114 redrawstatus! 115 close 116 redrawstatus! 117 endfunc 118 119 " Test get() function using default value. 120 121 " get({dict}, {key} [, {default}]) 122 func Test_get_dict() 123 let d = {'foo': 42} 124 call assert_equal(42, get(d, 'foo', 99)) 125 call assert_equal(999, get(d, 'bar', 999)) 126 endfunc 127 128 " get({list}, {idx} [, {default}]) 129 func Test_get_list() 130 let l = [1,2,3] 131 call assert_equal(1, get(l, 0, 999)) 132 call assert_equal(3, get(l, -1, 999)) 133 call assert_equal(999, get(l, 3, 999)) 134 endfunc 135 136 " get({blob}, {idx} [, {default}]) - in test_blob.vim 137 138 " get({lambda}, {what} [, {default}]) 139 func Test_get_lambda() 140 let l:L = {-> 42} 141 call assert_match('^<lambda>', get(l:L, 'name')) 142 call assert_equal(l:L, get(l:L, 'func')) 143 call assert_equal({'lambda has': 'no dict'}, get(l:L, 'dict', {'lambda has': 'no dict'})) 144 call assert_equal(0, get(l:L, 'dict')) 145 call assert_equal([], get(l:L, 'args')) 146 endfunc 147 148 func s:FooBar() 149 endfunc 150 151 " get({func}, {what} [, {default}]) 152 func Test_get_func() 153 let l:F = function('tr') 154 call assert_equal('tr', get(l:F, 'name')) 155 call assert_equal(l:F, get(l:F, 'func')) 156 call assert_equal({'required': 3, 'optional': 0, 'varargs': v:false}, 157 \ get(l:F, 'arity')) 158 159 let Fb_func = function('s:FooBar') 160 call assert_match('<SNR>\d\+_FooBar', get(Fb_func, 'name')) 161 call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false}, 162 \ get(Fb_func, 'arity')) 163 let Fb_ref = funcref('s:FooBar') 164 call assert_match('<SNR>\d\+_FooBar', get(Fb_ref, 'name')) 165 call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false}, 166 \ get(Fb_ref, 'arity')) 167 168 call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'})) 169 call assert_equal(0, get(l:F, 'dict')) 170 call assert_equal([], get(l:F, 'args')) 171 172 " Nvim doesn't have null functions 173 " let NF = test_null_function() 174 " call assert_equal('', get(NF, 'name')) 175 " call assert_equal(NF, get(NF, 'func')) 176 " call assert_equal(0, get(NF, 'dict')) 177 " call assert_equal([], get(NF, 'args')) 178 " call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false}, get(NF, 'arity')) 179 endfunc 180 181 " get({partial}, {what} [, {default}]) - in test_partial.vim 182 183 " vim: shiftwidth=2 sts=2 expandtab