xref: /vim-8.2.3635/src/testdir/check.vim (revision fb773a3e)
1source shared.vim
2source term_util.vim
3
4command -nargs=1 MissingFeature throw 'Skipped: ' .. <args> .. ' feature missing'
5
6" Command to check for the presence of a feature.
7command -nargs=1 CheckFeature call CheckFeature(<f-args>)
8func CheckFeature(name)
9  if !has(a:name, 1)
10    throw 'Checking for non-existent feature ' .. a:name
11  endif
12  if !has(a:name)
13    MissingFeature a:name
14  endif
15endfunc
16
17" Command to check for the absence of a feature.
18command -nargs=1 CheckNotFeature call CheckNotFeature(<f-args>)
19func CheckNotFeature(name)
20  if !has(a:name, 1)
21    throw 'Checking for non-existent feature ' .. a:name
22  endif
23  if has(a:name)
24    throw 'Skipped: ' .. a:name .. ' feature present'
25  endif
26endfunc
27
28" Command to check for the presence of a working option.
29command -nargs=1 CheckOption call CheckOption(<f-args>)
30func CheckOption(name)
31  if !exists('&' .. a:name)
32    throw 'Checking for non-existent option ' .. a:name
33  endif
34  if !exists('+' .. a:name)
35    throw 'Skipped: ' .. a:name .. ' option not supported'
36  endif
37endfunc
38
39" Command to check for the presence of a built-in function.
40command -nargs=1 CheckFunction call CheckFunction(<f-args>)
41func CheckFunction(name)
42  if !exists('?' .. a:name)
43    throw 'Checking for non-existent function ' .. a:name
44  endif
45  if !exists('*' .. a:name)
46    throw 'Skipped: ' .. a:name .. ' function missing'
47  endif
48endfunc
49
50" Command to check for the presence of an Ex command
51command -nargs=1 CheckCommand call CheckCommand(<f-args>)
52func CheckCommand(name)
53  if !exists(':' .. a:name)
54    throw 'Skipped: ' .. a:name .. ' command not supported'
55  endif
56endfunc
57
58" Command to check for the presence of a shell command
59command -nargs=1 CheckExecutable call CheckExecutable(<f-args>)
60func CheckExecutable(name)
61  if !executable(a:name)
62    throw 'Skipped: ' .. a:name .. ' program not executable'
63  endif
64endfunc
65
66" Command to check for the presence of python.  Argument should have been
67" obtained with PythonProg()
68func CheckPython(name)
69  if a:name == ''
70    throw 'Skipped: python command not available'
71  endif
72endfunc
73
74" Command to check for running on MS-Windows
75command CheckMSWindows call CheckMSWindows()
76func CheckMSWindows()
77  if !has('win32')
78    throw 'Skipped: only works on MS-Windows'
79  endif
80endfunc
81
82" Command to check for NOT running on MS-Windows
83command CheckNotMSWindows call CheckNotMSWindows()
84func CheckNotMSWindows()
85  if has('win32')
86    throw 'Skipped: does not work on MS-Windows'
87  endif
88endfunc
89
90" Command to check for running on Unix
91command CheckUnix call CheckUnix()
92func CheckUnix()
93  if !has('unix')
94    throw 'Skipped: only works on Unix'
95  endif
96endfunc
97
98" Command to check for running on Linix
99command CheckLinux call CheckLinux()
100func CheckLinux()
101  if !has('linux')
102    throw 'Skipped: only works on Linux'
103  endif
104endfunc
105
106" Command to check for not running on a BSD system.
107command CheckNotBSD call CheckNotBSD()
108func CheckNotBSD()
109  if has('bsd')
110    throw 'Skipped: does not work on BSD'
111  endif
112endfunc
113
114" Command to check that making screendumps is supported.
115" Caller must source screendump.vim
116command CheckScreendump call CheckScreendump()
117func CheckScreendump()
118  if !CanRunVimInTerminal()
119    throw 'Skipped: cannot make screendumps'
120  endif
121endfunc
122
123" Command to check that we can Run Vim in a terminal window
124command CheckRunVimInTerminal call CheckRunVimInTerminal()
125func CheckRunVimInTerminal()
126  if !CanRunVimInTerminal()
127    throw 'Skipped: cannot run Vim in a terminal window'
128  endif
129endfunc
130
131" Command to check that we can run the GUI
132command CheckCanRunGui call CheckCanRunGui()
133func CheckCanRunGui()
134  if !has('gui') || ($DISPLAY == "" && !has('gui_running'))
135    throw 'Skipped: cannot start the GUI'
136  endif
137endfunc
138
139" Command to check that we are using the GUI
140command CheckGui call CheckGui()
141func CheckGui()
142  if !has('gui_running')
143    throw 'Skipped: only works in the GUI'
144  endif
145endfunc
146
147" Command to check that not currently using the GUI
148command CheckNotGui call CheckNotGui()
149func CheckNotGui()
150  if has('gui_running')
151    throw 'Skipped: only works in the terminal'
152  endif
153endfunc
154
155" Command to check that test is not running as root
156command CheckNotRoot call CheckNotRoot()
157func CheckNotRoot()
158  if IsRoot()
159    throw 'Skipped: cannot run test as root'
160  endif
161endfunc
162
163" Command to check that the current language is English
164command CheckEnglish call CheckEnglish()
165func CheckEnglish()
166  if v:lang != "C" && v:lang !~ '^[Ee]n'
167      throw 'Skipped: only works in English language environment'
168  endif
169endfunc
170
171" Command to check that loopback device has IPv6 address
172command CheckIPv6 call CheckIPv6()
173func CheckIPv6()
174  if !has('ipv6')
175    throw 'Skipped: cannot use IPv6 networking'
176  endif
177  if !exists('s:ipv6_loopback')
178    let s:ipv6_loopback = s:CheckIPv6Loopback()
179  endif
180  if !s:ipv6_loopback
181    throw 'Skipped: no IPv6 address for loopback device'
182  endif
183endfunc
184
185func s:CheckIPv6Loopback()
186  if has('win32')
187    return system('netsh interface ipv6 show interface') =~? '\<Loopback\>'
188  elseif filereadable('/proc/net/if_inet6')
189    return (match(readfile('/proc/net/if_inet6'), '\slo$') >= 0)
190  elseif executable('ifconfig')
191    for dev in ['lo0', 'lo', 'loop']
192      " NOTE: On SunOS, need specify address family 'inet6' to get IPv6 info.
193      if system('ifconfig ' .. dev .. ' inet6 2>/dev/null') =~? '\<inet6\>'
194            \ || system('ifconfig ' .. dev .. ' 2>/dev/null') =~? '\<inet6\>'
195        return v:true
196      endif
197    endfor
198  else
199    " TODO: How to check it in other platforms?
200  endif
201  return v:false
202endfunc
203
204" Command to check for not running under ASAN
205command CheckNotAsan call CheckNotAsan()
206func CheckNotAsan()
207  if execute('version') =~# '-fsanitize=[a-z,]*\<address\>'
208    throw 'Skipped: does not work with ASAN'
209  endif
210endfunc
211
212" Command to check for satisfying any of the conditions.
213" e.g. CheckAnyOf Feature:bsd Feature:sun Linux
214command -nargs=+ CheckAnyOf call CheckAnyOf(<f-args>)
215func CheckAnyOf(...)
216  let excp = []
217  for arg in a:000
218    try
219      exe 'Check' .. substitute(arg, ':', ' ', '')
220      return
221    catch /^Skipped:/
222      let excp += [substitute(v:exception, '^Skipped:\s*', '', '')]
223    endtry
224  endfor
225  throw 'Skipped: ' .. join(excp, '; ')
226endfunc
227
228" Command to check for satisfying all of the conditions.
229" e.g. CheckAllOf Unix Gui Option:ballooneval
230command -nargs=+ CheckAllOf call CheckAllOf(<f-args>)
231func CheckAllOf(...)
232  for arg in a:000
233    exe 'Check' .. substitute(arg, ':', ' ', '')
234  endfor
235endfunc
236
237" vim: shiftwidth=2 sts=2 expandtab
238