From f9944442e685ef0cad7303e3cde29b441813941d Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Tue, 25 Jul 2023 22:30:30 -0700 Subject: [PATCH] Add markdown parser tests --- test/markdown_tests.vim | 196 ++++++++++++++++++++++++++++++++++++++++ test/run_tests.sh | 44 +++++---- 2 files changed, 220 insertions(+), 20 deletions(-) create mode 100644 test/markdown_tests.vim diff --git a/test/markdown_tests.vim b/test/markdown_tests.vim new file mode 100644 index 0000000..22f34c4 --- /dev/null +++ b/test/markdown_tests.vim @@ -0,0 +1,196 @@ +vim9script + +# Unit tests for the Github Flavored Markdown parser + +import '../autoload/lsp/markdown.vim' as md + +# Test for different markdowns +def g:Test_Markdown() + var tests: list>> = [ + [ + # Different headings + # Input text + [ + '# First level heading', + '## Second level heading', + '### Third level heading' + ], + # Expected text + [ + 'First level heading', + '', + 'Second level heading', + '', + 'Third level heading' + ], + # Expected text properties + [ + [{'col': 1, 'type': 'LspMarkdownHeading', 'length': 19}], + [], + [{'col': 1, 'type': 'LspMarkdownHeading', 'length': 20}], + [], + [{'col': 1, 'type': 'LspMarkdownHeading', 'length': 19}] + ] + ], + [ + # Bold text style + # Input text + [ + 'This **word** should be bold', + '', + '**This line should be bold**', + '', + 'This __word__ should be bold', + '', + '__This line should be bold__' + ], + # Expected text + [ + 'This word should be bold', + '', + 'This line should be bold', + '', + 'This word should be bold', + '', + 'This line should be bold' + ], + # Expected text properties + [ + [{'col': 6, 'type': 'LspMarkdownBold', 'length': 4}], + [], + [{'col': 1, 'type': 'LspMarkdownBold', 'length': 24}], + [], + [{'col': 6, 'type': 'LspMarkdownBold', 'length': 4}], + [], + [{'col': 1, 'type': 'LspMarkdownBold', 'length': 24}] + ] + ], + [ + # Italic text style + # Input text + [ + 'This *word* should be italic', + '', + '*This line should be italic*', + '', + 'This _word_ should be italic', + '', + '_This line should be italic_' + ], + # Expected text + [ + 'This word should be italic', + '', + 'This line should be italic', + '', + 'This word should be italic', + '', + 'This line should be italic' + ], + # Expected text properties + [ + [{'col': 6, 'type': 'LspMarkdownItalic', 'length': 4}], + [], + [{'col': 1, 'type': 'LspMarkdownItalic', 'length': 26}], + [], + [{'col': 6, 'type': 'LspMarkdownItalic', 'length': 4}], + [], + [{'col': 1, 'type': 'LspMarkdownItalic', 'length': 26}] + ], + ], + [ + # strikethrough text style + # Input text + [ + 'This ~word~ should be strikethrough', + '', + '~This line should be strikethrough~' + ], + # Expected text + [ + 'This word should be strikethrough', + '', + 'This line should be strikethrough' + ], + # Expected text properties + [ + [{'col': 6, 'type': 'LspMarkdownStrikeThrough', 'length': 4}], + [], + [{'col': 1, 'type': 'LspMarkdownStrikeThrough', 'length': 33}] + ] + ], + [ + # bold and nested italic text style + # Input text + [ + '**This _word_ should be bold and italic**', + ], + # Expected text + [ + 'This word should be bold and italic', + ], + # Expected text properties + [ + [ + {'col': 1, 'type': 'LspMarkdownBold', 'length': 35}, + {'col': 6, 'type': 'LspMarkdownItalic', 'length': 4} + ] + ] + ], + [ + # all bold and italic text style + # Input text + [ + '***This line should be all bold and italic***', + ], + # Expected text + [ + 'This line should be all bold and italic', + ], + # Expected text properties + [ + [ + {'col': 1, 'type': 'LspMarkdownItalic', 'length': 39}, + {'col': 1, 'type': 'LspMarkdownBold', 'length': 39} + ] + ] + ], + [ + # quoted text + # FIXME: The text is not quoted + # Input text + [ + 'Text that is not quoted', + '> quoted text' + ], + # Expected text + [ + 'Text that is not quoted', + '', + 'quoted text' + ], + # Expected text properties + [ + [], [], [] + ] + ] + ] + + var doc: dict> + var text_result: list + var props_result: list>> + for t in tests + doc = md.ParseMarkdown(t[0]) + text_result = doc.content->deepcopy()->map((_, v) => v.text) + props_result = doc.content->deepcopy()->map((_, v) => v.props) + assert_equal(t[1], text_result, t[0]->string()) + assert_equal(t[2], props_result, t[0]->string()) + endfor +enddef + +# Only here to because the test runner needs it +def g:StartLangServer(): bool + return true +enddef + +# vim: shiftwidth=2 softtabstop=2 noexpandtab diff --git a/test/run_tests.sh b/test/run_tests.sh index 625ba6f..d5ecee0 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -2,7 +2,9 @@ # Script to run the unit-tests for the LSP Vim plugin -VIMPRG=${VIMPRG:=$(which vim)} +#VIMPRG=${VIMPRG:=$(which vim)} +export VIMPRG=/home/yega/Documents/vim/vim9/vim/src/vim +export VIMRUNTIME=/home/yega/Documents/vim/vim9/vim/runtime if [ -z "$VIMPRG" ]; then echo "ERROR: vim (\$VIMPRG) is not found in PATH" exit 1 @@ -10,40 +12,42 @@ fi VIM_CMD="$VIMPRG -u NONE -U NONE -i NONE --noplugin -N --not-a-term" -TESTS="clangd_tests.vim tsserver_tests.vim gopls_tests.vim not_lspserver_related_tests.vim" +TESTS="clangd_tests.vim tsserver_tests.vim gopls_tests.vim not_lspserver_related_tests.vim markdown_tests.vim" RunTestsInFile() { - testfile=$1 - echo "Running tests in $testfile" - $VIM_CMD -c "let g:TestName='$testfile'" -S runner.vim + testfile=$1 + echo "Running tests in $testfile" + $VIM_CMD -c "let g:TestName='$testfile'" -S runner.vim - if ! [ -f results.txt ]; then - echo "ERROR: Test results file 'results.txt' is not found." - exit 2 - fi + if ! [ -f results.txt ]; then + echo "ERROR: Test results file 'results.txt' is not found." + exit 2 + fi - cat results.txt + cat results.txt - if grep -qw FAIL results.txt; then - echo "ERROR: Some test(s) in $testfile failed." - exit 3 - fi + if grep -qw FAIL results.txt; then + echo "ERROR: Some test(s) in $testfile failed." + exit 3 + fi - echo "SUCCESS: All the tests in $testfile passed." - echo + echo "SUCCESS: All the tests in $testfile passed." + echo } for testfile in $TESTS do - RunTestsInFile $testfile + RunTestsInFile $testfile done for encoding in "utf-8" "utf-16" "utf-32" do - export LSP_OFFSET_ENCODING=$encoding - echo "LSP offset encoding: $LSP_OFFSET_ENCODING" - RunTestsInFile clangd_offsetencoding.vim + export LSP_OFFSET_ENCODING=$encoding + echo "LSP offset encoding: $LSP_OFFSET_ENCODING" + RunTestsInFile clangd_offsetencoding.vim done echo "SUCCESS: All the tests passed." exit 0 + +# vim: shiftwidth=2 softtabstop=2 noexpandtab -- 2.44.0