1" Tests for the getbufinfo(), getwininfo() and gettabinfo() functions
2source check.vim
3
4func Test_getbufwintabinfo()
5  CheckFeature quickfix
6
7  edit Xtestfile1
8  edit Xtestfile2
9  let buflist = getbufinfo()
10  call assert_equal(2, len(buflist))
11  call assert_match('Xtestfile1', buflist[0].name)
12  call assert_match('Xtestfile2', getbufinfo('Xtestfile2')[0].name)
13  call assert_equal([], getbufinfo(2016))
14  edit Xtestfile1
15  hide edit Xtestfile2
16  hide enew
17  call assert_equal(3, len(getbufinfo({'bufloaded':1})))
18
19  set tabstop&vim
20  let b:editor = 'vim'
21  let l = getbufinfo('%')
22  call assert_equal(bufnr('%'), l[0].bufnr)
23  call assert_equal('vim', l[0].variables.editor)
24  call assert_notequal(-1, index(l[0].windows, '%'->bufwinid()))
25
26  " Test for getbufinfo() with 'bufmodified'
27  call assert_equal(0, len(getbufinfo({'bufmodified' : 1})))
28  call setbufline('Xtestfile1', 1, ["Line1"])
29  let l = getbufinfo({'bufmodified' : 1})
30  call assert_equal(1, len(l))
31  call assert_equal(bufnr('Xtestfile1'), l[0].bufnr)
32
33  if has('signs')
34    call append(0, ['Linux', 'Windows', 'Mac'])
35    sign define Mark text=>> texthl=Search
36    exe "sign place 2 line=3 name=Mark buffer=" . bufnr('%')
37    let l = getbufinfo('%')
38    call assert_equal(2, l[0].signs[0].id)
39    call assert_equal(3, l[0].signs[0].lnum)
40    call assert_equal('Mark', l[0].signs[0].name)
41    sign unplace *
42    sign undefine Mark
43    enew!
44  endif
45
46  only
47  let w1_id = win_getid()
48  new
49  let w2_id = win_getid()
50  tabnew | let w3_id = win_getid()
51  new | let w4_id = win_getid()
52  vert new | let w5_id = win_getid()
53  eval 'green'->setwinvar(0, 'signal')
54  tabfirst
55  let winlist = getwininfo()
56  call assert_equal(5, len(winlist))
57  call assert_equal(winwidth(1), winlist[0].width)
58  call assert_equal(1, winlist[0].wincol)
59  " tabline adds one row in terminal, not in GUI
60  let tablineheight = winlist[0].winrow == 2 ? 1 : 0
61  call assert_equal(tablineheight + 1, winlist[0].winrow)
62
63  call assert_equal(winbufnr(2), winlist[1].bufnr)
64  call assert_equal(winheight(2), winlist[1].height)
65  call assert_equal(1, winlist[1].wincol)
66  call assert_equal(tablineheight + winheight(1) + 2, winlist[1].winrow)
67
68  call assert_equal(1, winlist[2].winnr)
69  call assert_equal(tablineheight + 1, winlist[2].winrow)
70  call assert_equal(1, winlist[2].wincol)
71
72  call assert_equal(winlist[2].width + 2, winlist[3].wincol)
73  call assert_equal(1, winlist[4].wincol)
74
75  call assert_equal(1, winlist[0].tabnr)
76  call assert_equal(1, winlist[1].tabnr)
77  call assert_equal(2, winlist[2].tabnr)
78  call assert_equal(2, winlist[3].tabnr)
79  call assert_equal(2, winlist[4].tabnr)
80
81  call assert_equal('green', winlist[2].variables.signal)
82  call assert_equal(w4_id, winlist[3].winid)
83  let winfo = w5_id->getwininfo()[0]
84  call assert_equal(2, winfo.tabnr)
85  call assert_equal([], getwininfo(3))
86
87  call settabvar(1, 'space', 'build')
88  let tablist = gettabinfo()
89  call assert_equal(2, len(tablist))
90  call assert_equal(3, len(tablist[1].windows))
91  call assert_equal(2, tablist[1].tabnr)
92  call assert_equal('build', tablist[0].variables.space)
93  call assert_equal(w2_id, tablist[0].windows[0])
94  call assert_equal([], 3->gettabinfo())
95
96  tabonly | only
97
98  lexpr ''
99  lopen
100  copen
101  let winlist = getwininfo()
102  call assert_false(winlist[0].quickfix)
103  call assert_false(winlist[0].loclist)
104  call assert_true(winlist[1].quickfix)
105  call assert_true(winlist[1].loclist)
106  call assert_true(winlist[2].quickfix)
107  call assert_false(winlist[2].loclist)
108  wincmd t | only
109endfunc
110
111function Test_get_buf_options()
112  let opts = bufnr()->getbufvar('&')
113  call assert_equal(v:t_dict, type(opts))
114  call assert_equal(8, opts.tabstop)
115endfunc
116
117function Test_get_win_options()
118  if has('folding')
119    set foldlevel=999
120  endif
121  set list
122  let opts = getwinvar(1, '&')
123  call assert_equal(v:t_dict, type(opts))
124  call assert_equal(0, opts.linebreak)
125  call assert_equal(1, opts.list)
126  if has('folding')
127    call assert_equal(999, opts.foldlevel)
128  endif
129  if has('signs')
130    call assert_equal('auto', opts.signcolumn)
131  endif
132
133  let opts = gettabwinvar(1, 1, '&')
134  call assert_equal(v:t_dict, type(opts))
135  call assert_equal(0, opts.linebreak)
136  call assert_equal(1, opts.list)
137  if has('signs')
138    call assert_equal('auto', opts.signcolumn)
139  endif
140  set list&
141  if has('folding')
142    set foldlevel=0
143  endif
144endfunc
145
146function Test_getbufinfo_lastused()
147  call test_settime(1234567)
148  edit Xtestfile1
149  enew
150  call test_settime(7654321)
151  edit Xtestfile2
152  enew
153  call assert_equal(getbufinfo('Xtestfile1')[0].lastused, 1234567)
154  call assert_equal(getbufinfo('Xtestfile2')[0].lastused, 7654321)
155  call test_settime(0)
156endfunc
157
158func Test_getbufinfo_lines()
159  new Xfoo
160  call setline(1, ['a', 'bc', 'd'])
161  let bn = bufnr('%')
162  hide
163  call assert_equal(3, getbufinfo(bn)[0]["linecount"])
164  edit Xfoo
165  bw!
166endfunc
167