xref: /vim-8.2.3635/src/testdir/test_getvar.vim (revision ceb56ddb)
1" Tests for getwinvar(), gettabvar(), gettabwinvar() and get().
2
3func 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  unlet def_str
26
27  " test for gettabvar()
28  tabnew
29  tabnew
30  let t:var_list = [1, 2, 3]
31  let t:other = 777
32  let def_list = [4, 5, 6, 7]
33  tabrewind
34  call assert_equal([1, 2, 3], 3->gettabvar('var_list'))
35  call assert_equal([1, 2, 3], gettabvar(3, 'var_list', def_list))
36  call assert_equal({'var_list': [1, 2, 3], 'other': 777}, gettabvar(3, ''))
37  call assert_equal({'var_list': [1, 2, 3], 'other': 777},
38					\ gettabvar(3, '', def_list))
39
40  tablast
41  unlet t:var_list
42  tabrewind
43  call assert_equal([4, 5, 6, 7], gettabvar(3, 'var_list', def_list))
44  call assert_equal('', gettabvar(9, ''))
45  call assert_equal([4, 5, 6, 7], gettabvar(9, '', def_list))
46  call assert_equal('', gettabvar(3, '&nu'))
47  call assert_equal([4, 5, 6, 7], gettabvar(3, '&nu', def_list))
48  unlet def_list
49  tabonly
50
51  " test for gettabwinvar()
52  tabnew
53  tabnew
54  tabprev
55  split
56  split
57  wincmd w
58  vert split
59  wincmd w
60  let w:var_dict = {'dict': 'tabwin'}
61  let def_dict = {'dict2': 'newval'}
62  wincmd b
63  tabrewind
64  call assert_equal({'dict': 'tabwin'}, 2->gettabwinvar(3, 'var_dict'))
65  call assert_equal({'dict': 'tabwin'},
66				\ gettabwinvar(2, 3, 'var_dict', def_dict))
67  call assert_equal({'var_dict': {'dict': 'tabwin'}}, gettabwinvar(2, 3, ''))
68  call assert_equal({'var_dict': {'dict': 'tabwin'}},
69				\ gettabwinvar(2, 3, '', def_dict))
70
71  tabnext
72  3wincmd w
73  unlet w:var_dict
74  tabrewind
75  call assert_equal({'dict2': 'newval'},
76				\ gettabwinvar(2, 3, 'var_dict', def_dict))
77  call assert_equal({}, gettabwinvar(2, 3, ''))
78  call assert_equal({}, gettabwinvar(2, 3, '', def_dict))
79  call assert_equal("", gettabwinvar(2, 9, ''))
80  call assert_equal({'dict2': 'newval'}, gettabwinvar(2, 9, '', def_dict))
81  call assert_equal('', gettabwinvar(9, 3, ''))
82  call assert_equal({'dict2': 'newval'}, gettabwinvar(9, 3, '', def_dict))
83
84  unlet def_dict
85
86  call assert_equal("", gettabwinvar(9, 2020, ''))
87  call assert_equal('', gettabwinvar(2, 3, '&nux'))
88  call assert_equal(1, gettabwinvar(2, 3, '&nux', 1))
89  tabonly
90endfunc
91
92" It was discovered that "gettabvar()" would fail if called from within the
93" tabline when the user closed a window.  This test confirms the fix.
94func Test_gettabvar_in_tabline()
95  let t:var_str = 'value'
96
97  set tabline=%{assert_equal('value',gettabvar(1,'var_str'))}
98  set showtabline=2
99
100  " Simulate the user opening a split (which becomes window #1) and then
101  " closing the split, which triggers the redrawing of the tabline.
102  leftabove split
103  redrawstatus!
104  close
105  redrawstatus!
106endfunc
107
108" Test get() function using default value.
109
110" get({dict}, {key} [, {default}])
111func Test_get_dict()
112  let d = {'foo': 42}
113  call assert_equal(42, get(d, 'foo', 99))
114  call assert_equal(999, get(d, 'bar', 999))
115endfunc
116
117" get({list}, {idx} [, {default}])
118func Test_get_list()
119  let l = [1,2,3]
120  call assert_equal(1, get(l, 0, 999))
121  call assert_equal(3, get(l, -1, 999))
122  call assert_equal(999, get(l, 3, 999))
123endfunc
124
125" get({blob}, {idx} [, {default}]) - in test_blob.vim
126
127" get({lambda}, {what} [, {default}])
128func Test_get_lambda()
129  let l:L = {-> 42}
130  call assert_match('^<lambda>', get(l:L, 'name'))
131  call assert_equal(l:L, get(l:L, 'func'))
132  call assert_equal({'lambda has': 'no dict'}, get(l:L, 'dict', {'lambda has': 'no dict'}))
133  call assert_equal(0, get(l:L, 'dict'))
134  call assert_equal([], get(l:L, 'args'))
135endfunc
136
137" get({func}, {what} [, {default}])
138func Test_get_func()
139  let l:F = function('tr')
140  call assert_equal('tr', get(l:F, 'name'))
141  call assert_equal(l:F, get(l:F, 'func'))
142  call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'}))
143  call assert_equal(0, get(l:F, 'dict'))
144  call assert_equal([], get(l:F, 'args'))
145  let NF = test_null_function()
146  call assert_equal('', get(NF, 'name'))
147  call assert_equal(NF, get(NF, 'func'))
148  call assert_equal(0, get(NF, 'dict'))
149  call assert_equal([], get(NF, 'args'))
150endfunc
151
152" get({partial}, {what} [, {default}]) - in test_partial.vim
153