|
1 | 1 | vim9script |
2 | 2 |
|
3 | | -# The global variable TestName should be set to the name of the file |
4 | | -# containing the tests. |
| 3 | +# The global variable g:TestFiles is a list containing all the tests filenames. |
| 4 | +if exists('g:TestFiles') == 0 |
| 5 | + g:TestFiles = ['test_markdown_extras.vim', 'test_utils.vim'] |
| 6 | +endif |
5 | 7 |
|
6 | | -# Uncomment the following to debug this script |
7 | | -# g:TestName = 'test_markdown_extras.vim' |
| 8 | +delete('results.txt') |
8 | 9 |
|
9 | | -def RunTests() |
| 10 | +def RunTests(test_file: string) |
10 | 11 | set nomore |
11 | 12 | set debug=beep |
12 | | - delete('results.txt') |
13 | | - |
14 | | - # Get the list of test functions in this file and call them |
15 | | - var fns: list<string> = execute('def /^Test_') |
16 | | - ->split("\n") |
17 | | - ->map("v:val->substitute('^def ', '', '')") |
18 | | - ->sort() |
19 | | - if fns->empty() |
| 13 | + |
| 14 | + writefile([$'o {test_file}'], 'results.txt', 'a') |
| 15 | + |
| 16 | + # Load test functions in memory |
| 17 | + execute $"source {test_file}" |
| 18 | + var read_test_file = readfile(test_file) |
| 19 | + |
| 20 | + # Get test functions names |
| 21 | + var all_functions = copy(read_test_file) |
| 22 | + ->filter('v:val =~ "^def g:"') |
| 23 | + ->map("v:val->substitute('^def g:', '', '')") |
| 24 | + ->sort() |
| 25 | + |
| 26 | + # Check is user defined some function name erroneously |
| 27 | + var wrong_test_functions = copy(all_functions)->filter('v:val !~ "Test_"') |
| 28 | + echom "wrong_functions: " .. string(wrong_test_functions) |
| 29 | + if !empty(wrong_test_functions) |
| 30 | + writefile([$'WARNING: The following tests are skipped: {wrong_test_functions}'], 'results.txt', 'a') |
| 31 | + writefile([''], 'results.txt', 'a') |
| 32 | + endif |
| 33 | + |
| 34 | + # Pick the good functions |
| 35 | + var test_functions = copy(all_functions)->filter('v:val =~ "Test_"') |
| 36 | + echom "test_functions: " .. string(test_functions) |
| 37 | + if test_functions->empty() |
20 | 38 | # No tests are found |
21 | | - writefile(['No tests are found'], 'results.txt') |
| 39 | + writefile([$'No tests are found in {test_file}'], 'results.txt', 'a') |
22 | 40 | return |
23 | 41 | endif |
24 | 42 |
|
25 | | - # Execute the functions |
26 | | - for f in fns |
| 43 | + # Execute the test functions |
| 44 | + # writefile(['Executed test:'], 'results.txt', 'a') |
| 45 | + for test in test_functions |
| 46 | + echom "current test: " .. test |
27 | 47 | v:errors = [] |
28 | 48 | v:errmsg = '' |
| 49 | + exe $'call {test}' |
29 | 50 | try |
30 | 51 | :%bw! |
31 | | - exe $'g:{f}' |
| 52 | + exe $'call {test}' |
32 | 53 | catch |
33 | | - add(v:errors, $'Error: Test {f} failed with exception {v:exception} at {v:throwpoint}') |
| 54 | + add(v:errors, $'Error: Test {test} failed with exception {v:exception} at {v:throwpoint}') |
34 | 55 | endtry |
35 | 56 |
|
36 | 57 | if v:errmsg != '' |
37 | | - add(v:errors, $'Error: Test {f} generated error {v:errmsg}') |
| 58 | + add(v:errors, $'Error: Test {test} generated error {v:errmsg}') |
38 | 59 | endif |
39 | 60 | if !v:errors->empty() |
40 | 61 | writefile(v:errors, 'results.txt', 'a') |
41 | | - writefile([$'{f}: FAIL'], 'results.txt', 'a') |
| 62 | + writefile([$'{test}: FAIL'], 'results.txt', 'a') |
42 | 63 | else |
43 | | - writefile([$'{f}: pass'], 'results.txt', 'a') |
| 64 | + writefile([$'{test}: pass'], 'results.txt', 'a') |
44 | 65 | endif |
45 | 66 | endfor |
| 67 | + # Test results separator |
46 | 68 | enddef |
47 | 69 |
|
48 | | -try |
49 | | - exe $'source {g:TestName}' |
50 | | - RunTests() |
51 | | -catch |
52 | | - writefile(['FAIL: Tests in ' .. g:TestName .. ' failed with exception ' .. v:exception .. ' at ' .. v:throwpoint], 'results.txt', 'a') |
53 | | -endtry |
| 70 | +# To test in stand-alone, remove the try block from the following |
| 71 | +for test_file in g:TestFiles |
| 72 | + try |
| 73 | + RunTests(test_file) |
| 74 | + writefile([''], 'results.txt', 'a') |
| 75 | + catch |
| 76 | + writefile(['FAIL: Tests in ' .. test_file .. ' failed with exception ' |
| 77 | + \ .. v:exception .. ' at ' .. v:throwpoint], 'results.txt', 'a') |
| 78 | + endtry |
| 79 | +endfor |
54 | 80 |
|
55 | 81 | qall! |
56 | 82 |
|
|
0 commit comments