Skip to content

Commit fc821f4

Browse files
committed
Extended to multiple tests
1 parent 084df5b commit fc821f4

File tree

5 files changed

+73
-37
lines changed

5 files changed

+73
-37
lines changed

test/common.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vim9scrip
1+
vim9script
22
# Common routines used for running the unit tests
33

44

test/results.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
o test_markdown_extras.vim
12
Test_check_uncheck_todo_keybinding(): pass
23
Test_markdown_lists(): pass
4+
5+
o test_utils.vim
6+
WARNING: The following tests are skipped: ['Sto_cazzo()']
7+
8+
Test_sto_cazzo(): pass
9+

test/run_tests.cmd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ REM
1616
echo set runtimepath+=..
1717
echo set runtimepath+=../after
1818
echo filetype plugin indent on
19-
echo/
2019
) >> "%VIMRC%"
2120

2221
SET "VIM_CMD=%VIMPRG% --clean -u %VIMRC% -i NONE --not-a-term"
@@ -31,9 +30,11 @@ REM Display the contents of VIMRC (for debugging purposes)
3130
echo/
3231
echo ----- dummy_vimrc content -------
3332
type "%VIMRC%"
33+
echo/
3434

3535
REM Run Vim with the specified configuration and additional commands
36-
%VIM_CMD% -c "vim9cmd g:TestName = 'test_markdown_extras.vim'" -S "runner.vim"
36+
SET "TEST_FILES=['test_markdown_extras.vim', 'test_utils.vim']"
37+
%VIM_CMD% -c "vim9cmd g:TestFiles = %TEST_FILES%" -S "runner.vim"
3738
REM If things go wrong uncomment the following line and see e.g. if the
3839
REM vimrc_for_test is valid, check :messages and so on.
3940
REM %VIM_CMD% -c "vim9cmd g:TestName = 'test_markdown_extras.vim'" -c "e README.md"
@@ -42,19 +43,20 @@ REM Check the exit code of Vim command
4243
if %ERRORLEVEL% EQU 0 (
4344
echo Vim command executed successfully.
4445
) else (
46+
echo/
4547
echo ERROR: Vim command failed with exit code %ERRORLEVEL%.
4648
del %VIMRC%
4749
exit /b 1
4850
)
4951

50-
REM REM Check test results
52+
REM Check test results
5153
echo ----------------------------------
5254
echo MARKDOWN_EXTRAS unit-test results:
5355
echo/
5456
type results.txt
55-
echo/
57+
echo ----------------------------------
5658

57-
REM REM Check for FAIL in results.txt
59+
REM Check for FAIL in results.txt
5860
findstr /I "FAIL" results.txt > nul 2>&1
5961
if %ERRORLEVEL% EQU 0 (
6062
echo ERROR: Some test failed.
@@ -63,6 +65,7 @@ if %ERRORLEVEL% EQU 0 (
6365
) else (
6466
echo SUCCESS: All tests passed.
6567
)
68+
echo/
6669

6770
REM REM Exit script with success
6871
del %VIMRC%

test/run_tests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,22 @@ echo "">> "$VIMRC"
2828
echo "set runtimepath+=.." >> "$VIMRC"
2929
echo "set runtimepath+=../after" >> "$VIMRC"
3030
echo "filetype plugin on" >> "$VIMRC"
31-
echo "" >> "$VIMRC"
3231

3332
echo "----- vimrc content ---------"
3433
cat $VIMRC
35-
echo "-----------------------------"
34+
echo ""
3635
# Construct the VIM_CMD with correct variable substitution and quoting
3736
# VIM_CMD="$VIM_PRG -u $VIMRC -U NONE -i NONE --noplugin -N --not-a-term"
3837
VIM_CMD="$VIM_PRG --clean -u $VIMRC -i NONE --not-a-term"
3938

4039
# Add space separated tests, i.e. "test_markdown_extras.vim test_pippo.vim etc"
41-
TESTS="test_markdown_extras.vim"
40+
TESTS=["test_markdown_extras.vim", "test_utils.vim"]
4241

4342
RunTestsInFile() {
4443
testfile=$1
4544
echo "Running tests in $testfile"
4645
# If you want to see the output remove the & from the line below
47-
eval $VIM_CMD " -c \"vim9cmd g:TestName = '$testfile'\" -S runner.vim"
46+
eval $VIM_CMD " -c \"vim9cmd g:TestName = ['$testfile']\" -S runner.vim"
4847

4948
if ! [ -f results.txt ]; then
5049
echo "ERROR: Test results file 'results.txt' is not found."
@@ -75,6 +74,7 @@ do
7574
RunTestsInFile $testfile
7675
done
7776

77+
echo "--------------------------------"
7878
echo "SUCCESS: All the tests passed."
7979
# UBA: uncomment the line below
8080
if [ "$GITHUB" -eq 1 ]; then

test/runner.vim

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,82 @@
11
vim9script
22

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
57

6-
# Uncomment the following to debug this script
7-
# g:TestName = 'test_markdown_extras.vim'
8+
delete('results.txt')
89

9-
def RunTests()
10+
def RunTests(test_file: string)
1011
set nomore
1112
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()
2038
# No tests are found
21-
writefile(['No tests are found'], 'results.txt')
39+
writefile([$'No tests are found in {test_file}'], 'results.txt', 'a')
2240
return
2341
endif
2442

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
2747
v:errors = []
2848
v:errmsg = ''
49+
exe $'call {test}'
2950
try
3051
:%bw!
31-
exe $'g:{f}'
52+
exe $'call {test}'
3253
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}')
3455
endtry
3556

3657
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}')
3859
endif
3960
if !v:errors->empty()
4061
writefile(v:errors, 'results.txt', 'a')
41-
writefile([$'{f}: FAIL'], 'results.txt', 'a')
62+
writefile([$'{test}: FAIL'], 'results.txt', 'a')
4263
else
43-
writefile([$'{f}: pass'], 'results.txt', 'a')
64+
writefile([$'{test}: pass'], 'results.txt', 'a')
4465
endif
4566
endfor
67+
# Test results separator
4668
enddef
4769

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
5480

5581
qall!
5682

0 commit comments

Comments
 (0)