xref: /vim-8.2.3635/src/testdir/test_getvar.vim (revision d7df2798)
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(2, 3, '&nux'))
87  call assert_equal(1, gettabwinvar(2, 3, '&nux', 1))
88  tabonly
89endfunc
90
91" It was discovered that "gettabvar()" would fail if called from within the
92" tabline when the user closed a window.  This test confirms the fix.
93func Test_gettabvar_in_tabline()
94  let t:var_str = 'value'
95
96  set tabline=%{assert_equal('value',gettabvar(1,'var_str'))}
97  set showtabline=2
98
99  " Simulate the user opening a split (which becomes window #1) and then
100  " closing the split, which triggers the redrawing of the tabline.
101  leftabove split
102  redrawstatus!
103  close
104  redrawstatus!
105endfunc
106
107" Test get() function using default value.
108
109" get({dict}, {key} [, {default}])
110func Test_get_dict()
111  let d = {'foo': 42}
112  call assert_equal(42, get(d, 'foo', 99))
113  call assert_equal(999, get(d, 'bar', 999))
114endfunc
115
116" get({list}, {idx} [, {default}])
117func Test_get_list()
118  let l = [1,2,3]
119  call assert_equal(1, get(l, 0, 999))
120  call assert_equal(3, get(l, -1, 999))
121  call assert_equal(999, get(l, 3, 999))
122endfunc
123
124" get({blob}, {idx} [, {default}]) - in test_blob.vim
125
126" get({lambda}, {what} [, {default}])
127func Test_get_lambda()
128  let l:L = {-> 42}
129  call assert_match('^<lambda>', get(l:L, 'name'))
130  call assert_equal(l:L, get(l:L, 'func'))
131  call assert_equal({'lambda has': 'no dict'}, get(l:L, 'dict', {'lambda has': 'no dict'}))
132  call assert_equal(0, get(l:L, 'dict'))
133  call assert_equal([], get(l:L, 'args'))
134endfunc
135
136" get({func}, {what} [, {default}])
137func Test_get_func()
138  let l:F = function('tr')
139  call assert_equal('tr', get(l:F, 'name'))
140  call assert_equal(l:F, get(l:F, 'func'))
141  call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'}))
142  call assert_equal(0, get(l:F, 'dict'))
143  call assert_equal([], get(l:F, 'args'))
144endfunc
145
146" get({partial}, {what} [, {default}]) - in test_partial.vim
147