-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.vim
More file actions
executable file
·171 lines (148 loc) · 4.38 KB
/
test.vim
File metadata and controls
executable file
·171 lines (148 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/env -S bash -c '${VIMPROG-vim} -u NONE -i NONE -N -n -X -e -s -S "$0" <(IFS=$\'\n\'; echo "$*")'
function! s:run(package_dir) abort
set nohidden noswapfile
let args = filter(getline(1, line('$')), 'v:val != ""')
silent! %argdelete
silent! %bwipeout!
let &runtimepath .= ',' . a:package_dir
let &packpath .= ',' . a:package_dir
for test_file in globpath(a:package_dir, 'test/**/*_test.vim', 0, 1)
source `=test_file`
endfor
let script_paths = {}
for line in split(execute('0verbose scriptnames'), '\n')
let matches = matchlist(line, '^\s*\(\d\+\):\s\(.*\)')
if empty(matches)
continue
endif
let script_num = matches[1]
let script_path = fnamemodify(matches[2], ':p')
if stridx(script_path, a:package_dir . '/test/') == 0
let script_paths[script_num] = script_path
endif
endfor
let test_functions = s:shuffle(filter(
\ map(
\ split(execute('0verbose function'), '\n'),
\ { i, value ->
\ matchstr(value, '^function \zs<SNR>\d\+_test\%(_\w\+\)\?\>') }
\ ),
\ { i, value -> has_key(script_paths, matchstr(value, '^<SNR>\zs\d\+')) }
\ ))
echo 'running'
\ len(test_functions)
\ (len(test_functions) > 1 ? 'tests' : 'test')
\ "\n"
let failed = 0
let passed = 0
let ignored = 0
let filtered_out = 0
let errors = []
let start_time = reltime()
for test_function in test_functions
let script_num = matchstr(test_function, '^<SNR>\zs\d\+')
let script_name = fnamemodify(script_paths[script_num], ':.')
let test_name = substitute(test_function, '^<SNR>\d\+_', '', 'I')
let full_name = script_name . '::' . test_name
if max(map(copy(args), 'stridx(full_name, v:val)')) == -1
let filtered_out += 1
continue
endif
echon full_name ' ... '
let v:errors = []
try
let return_value = call(test_function, [])
catch
let return_value = -1
let message = join(split(v:throwpoint, '\.\.')[1:], "\n")
\ . "\n"
\ . v:exception
call add(errors, {
\ 'script_name': script_name,
\ 'test_name': test_name,
\ 'messages': [message],
\ })
endtry
if len(v:errors) > 0
let messages = map(
\ copy(v:errors),
\ { i, message -> substitute(
\ join(split(message, '\.\.'), "\n"),
\ '^\S\+\zs\s\zeline\s\d\+:',
\ "\n",
\ ''
\ )
\ }
\ )
call add(errors, {
\ 'script_name': script_name,
\ 'test_name': test_name,
\ 'messages': messages,
\ })
endif
if type(return_value) == v:t_string
call add(errors, {
\ 'script_name': script_name,
\ 'test_name': test_name,
\ 'messages': [return_value],
\ })
let ignored += 1
echon 'ignored' "\n"
elseif return_value == -1 || len(v:errors) > 0
let failed += 1
echon 'FAILED' "\n"
else
let passed += 1
echon 'ok' "\n"
endif
if len(getbufinfo({ 'buflisted': 1 })) > 1
\ || line('$') > 1
\ || col('$') > 1
\ || winnr('$') > 1
\ || tabpagenr('$') > 1
call add(errors, {
\ 'script_name': script_name,
\ 'test_name': test_name,
\ 'messages': [
\ 'CAUTION: Unclean buffers, windows, or tabs were found, therefore the execution of remaining tests has been aborted.'
\ ],
\ })
break
endif
endfor
let elapsed_time = substitute(reltimestr(reltime(start_time)),
\ '^\s*', '', '')
for error in errors
echo '----' error.script_name . '::' . error.test_name '----'
for message in error.messages
echo message "\n"
endfor
endfor
echo 'test result:'
\ (failed > 0 ? 'FAILED.' : 'ok.')
\ passed 'passed;'
\ failed 'failed;'
\ ignored 'ignored;'
\ filtered_out 'filtered out;'
\ 'finished in' (elapsed_time . 's')
\ "\n"
if failed > 0
cquit!
else
qall!
endif
endfunction
function! s:shuffle(elements) abort
let Rand = exists('*rand') ? function('rand') : { -> reltime()[1] }
let l = len(a:elements)
while l > 0
let i = Rand() % l
let l -= 1
let tmp = a:elements[i]
let a:elements[i] = a:elements[l]
let a:elements[l] = tmp
endwhile
return a:elements
endfunction
verbose echo matchstr(execute('version'), '^\n*\zs[^\n]\+') "\n"
verbose call s:run(expand('<sfile>:p:h'))