From bc6fb6addf20d50dc651cf6d5a35a4b33cd25d5a Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Thu, 28 Apr 2022 10:04:42 +0200 Subject: [PATCH 1/2] Add a few tests As a bit of a follow-up to #42, this adds tests for the syntax highlighting. Example zsh files are in testdata/*.zsh; after writing test files are verifying it's all good you can generate with "make update-tests", and you can run it with "make test". I included the testing.vim as a submodule here. --- .gitmodules | 3 ++ Makefile | 11 +++++- syntax_test.vim | 81 +++++++++++++++++++++++++++++++++++++++++++++ testdata/if.zsh | 26 +++++++++++++++ testdata/quotes.zsh | 13 ++++++++ testdata/var.zsh | 23 +++++++++++++ testing.vim | 1 + 7 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 syntax_test.vim create mode 100644 testdata/if.zsh create mode 100644 testdata/quotes.zsh create mode 100644 testdata/var.zsh create mode 160000 testing.vim diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5790886 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "testing.vim"] + path = testing.vim + url = git@github.com:arp242/testing.vim.git diff --git a/Makefile b/Makefile index 55e7541..2143a4e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,16 @@ zip: - @rm -f zsh-runtime.zip; find . -type f -name "*.vim" | zip -@ zsh-runtime.zip + @rm -f zsh-runtime.zip + @find . -type f -name "*.vim" -a -not -name '*_test.vim' -a -not -name 'tvim.vim' | zip -@ zsh-runtime.zip submit: @echo "Set environment variable '\$$MSG' to the tag message, like this:" @echo "MSG='My tag message'" @echo "git tag -f -s \`date +'%Y%m%d'\` -m \"\$$MSG\"" + +test: + @git submodule update + @./testing.vim/tvim test ./... + +update-tests: + @git submodule update + ./testing.vim/tvim gen-syn ./testdata/*.zsh > syntax_test.vim diff --git a/syntax_test.vim b/syntax_test.vim new file mode 100644 index 0000000..e83a278 --- /dev/null +++ b/syntax_test.vim @@ -0,0 +1,81 @@ +" This file is automatically generated by test-syntax from testing.vim + +fun! Test_if() abort + call TestSyntax(g:test_packdir . '/testdata/if.zsh', + \ [ + \ [['zshComment', 1, 50]], + \ [], + \ [['zshConditional', 1, 3], ['zshDeref', 6, 10], ['zshOperator', 12, 13], ['zshConditional', 14, 17]], + \ [], + \ [['zshConditional', 1, 5], ['zshDeref', 9, 13], ['zshOperator', 16, 17], ['zshConditional', 18, 21]], + \ [], + \ [['zshConditional', 1, 5], ['zshSubstDelim', 6, 8], ['zshDeref', 9, 13], ['zshSubstDelim', 14, 14], ['zshOperator', 16, 17], ['zshConditional', 18, 21]], + \ [], + \ [['zshConditional', 1, 2]], + \ [], + \ [['zshConditional', 1, 3], ['zshSubstDelim', 4, 6], ['zshSubstDelim', 14, 14], ['zshOperator', 15, 16], ['zshDelimiter', 17, 18]], + \ [], + \ [['zshConditional', 1, 5], ['zshSubstDelim', 9, 11], ['zshSubstDelim', 19, 20], ['zshNumber', 25, 27], ['zshOperator', 30, 31], ['zshConditional', 32, 35]], + \ [], + \ [['zshConditional', 1, 5], ['zshSubstDelim', 6, 8], ['zshSubstDelim', 9, 11], ['zshSubstDelim', 19, 20], ['zshNumber', 23, 25], ['zshSubstDelim', 26, 26], ['zshOperator', 28, 29], ['zshConditional', 30, 33]], + \ [], + \ [['zshConditional', 1, 2]], + \ [], + \ [['zshSubstDelim', 4, 6], ['zshSubstDelim', 14, 15], ['zshNumber', 20, 22], ['zshOperator', 26, 28], ['zshCommands', 29, 34]], + \ [['zshSubstDelim', 1, 3], ['zshSubstDelim', 4, 6], ['zshSubstDelim', 14, 15], ['zshNumber', 18, 20], ['zshSubstDelim', 21, 23], ['zshOperator', 26, 28], ['zshCommands', 29, 34]], + \ [], + \ [['zshComment', 1, 38]], + \ [['zshOperator', 12, 12], ['zshSubstDelim', 14, 14], ['zshNumber', 16, 17], ['zshNumber', 18, 18], ['zshSubstDelim', 19, 19], ['zshOperator', 21, 23], ['zshCommands', 24, 29]], + \ [], + \ [['zshComment', 1, 1], ['zshTodo', 3, 3], ['zshComment', 7, 71]], + \ [['zshVariableDef', 3, 4], ['zshOperator', 9, 11], ['zshVariableDef', 13, 14]], + \ ]) +endfun + +fun! Test_quotes() abort + call TestSyntax(g:test_packdir . '/testdata/quotes.zsh', + \ [ + \ [['zshComment', 1, 15]], + \ [], + \ [['zshStringDelimiter', 2, 2], ['zshString', 3, 3], ['zshStringDelimiter', 9, 10], ['zshStringDelimiter', 13, 13], ['zshString', 14, 14], ['zshStringDelimiter', 23, 24], ['zshStringDelimiter', 27, 27], ['zshString', 28, 28], ['zshStringDelimiter', 40, 41], ['zshStringDelimiter', 44, 44], ['zshString', 45, 45], ['zshStringDelimiter', 62, 62]], + \ [['zshStringDelimiter', 2, 2], ['zshString', 3, 3], ['zshStringDelimiter', 9, 10], ['zshStringDelimiter', 13, 13], ['zshString', 14, 14], ['zshDeref', 19, 19], ['zshStringDelimiter', 23, 24], ['zshStringDelimiter', 27, 27], ['zshString', 28, 28], ['zshSubstDelim', 35, 35], ['zshString', 37, 37], ['zshSubstDelim', 39, 39], ['zshStringDelimiter', 40, 41], ['zshStringDelimiter', 44, 44], ['zshString', 45, 45], ['zshQuoted', 50, 50], ['zshString', 52, 52], ['zshQuoted', 53, 53], ['zshString', 55, 55], ['zshQuoted', 58, 58], ['zshString', 60, 60], ['zshStringDelimiter', 62, 62], ['zshComment', 63, 63], ['zshTodo', 66, 66], ['zshComment', 70, 103]], + \ [['zshStringDelimiter', 1, 1], ['zshPOSIXString', 3, 3], ['zshStringDelimiter', 9, 10], ['zshStringDelimiter', 12, 12], ['zshPOSIXString', 14, 14], ['zshStringDelimiter', 23, 24], ['zshStringDelimiter', 26, 26], ['zshPOSIXString', 28, 28], ['zshStringDelimiter', 40, 41], ['zshStringDelimiter', 43, 43], ['zshPOSIXString', 45, 45], ['zshQuoted', 50, 50], ['zshPOSIXString', 52, 52], ['zshPOSIXQuoted', 53, 53], ['zshPOSIXString', 57, 57], ['zshPOSIXQuoted', 58, 58], ['zshStringDelimiter', 62, 62]], + \ [['zshSubstDelim', 2, 2], ['zshOldSubst', 3, 3], ['zshSubstDelim', 9, 10], ['zshSubstDelim', 13, 13], ['zshOldSubst', 14, 14], ['zshDeref', 19, 19], ['zshSubstDelim', 23, 24], ['zshSubstDelim', 27, 27], ['zshOldSubst', 28, 28], ['zshSubstDelim', 35, 35], ['zshOldSubst', 37, 37], ['zshSubstDelim', 39, 41], ['zshSubstDelim', 44, 44], ['zshOldSubst', 45, 45], ['zshQuoted', 50, 50], ['zshOldSubst', 52, 52], ['zshPOSIXQuoted', 53, 53], ['zshOldSubst', 57, 57], ['zshPOSIXQuoted', 58, 58], ['zshSubstDelim', 62, 62]], + \ [], + \ [['zshStringDelimiter', 1, 1], ['zshString', 2, 2], ['zshStringDelimiter', 8, 9], ['zshStringDelimiter', 10, 10], ['zshSubstDelim', 11, 11], ['zshOldSubst', 12, 12], ['zshSubstDelim', 16, 16], ['zshStringDelimiter', 17, 17]], + \ [['zshStringDelimiter', 1, 1], ['zshString', 2, 2], ['zshStringDelimiter', 8, 9], ['zshStringDelimiter', 10, 10], ['zshString', 11, 11], ['zshStringDelimiter', 17, 17]], + \ [], + \ [['zshStringDelimiter', 2, 2], ['zshString', 3, 3], ['zshQuoted', 11, 11], ['zshString', 13, 13], ['zshQuoted', 14, 14], ['zshString', 16, 16], ['zshQuoted', 17, 17], ['zshString', 19, 19], ['zshQuoted', 20, 20], ['zshString', 22, 22], ['zshStringDelimiter', 29, 29]], + \ [['zshStringDelimiter', 2, 2], ['zshString', 3, 3], ['zshStringDelimiter', 15, 16], ['zshQuoted', 17, 19], ['zshQuoted', 20, 22], ['zshStringDelimiter', 29, 29], ['zshString', 30, 30], ['zshStringDelimiter', 57, 58]], + \ [['zshStringDelimiter', 1, 1], ['zshPOSIXString', 3, 3], ['zshQuoted', 11, 11], ['zshPOSIXString', 13, 13], ['zshQuoted', 14, 14], ['zshPOSIXString', 16, 16], ['zshQuoted', 17, 17], ['zshPOSIXString', 19, 19], ['zshQuoted', 20, 20], ['zshPOSIXString', 22, 22], ['zshStringDelimiter', 29, 29]], + \ ]) +endfun + +fun! Test_var() abort + call TestSyntax(g:test_packdir . '/testdata/var.zsh', + \ [ + \ [['zshComment', 1, 16]], + \ [], + \ [['zshComment', 1, 29]], + \ [['zshVariableDef', 1, 2]], + \ [['zshVariableDef', 1, 4]], + \ [['zshTypes', 1, 6], ['zshVariableDef', 7, 8]], + \ [['zshTypes', 1, 8], ['zshVariableDef', 12, 13]], + \ [], + \ [['zshCommands', 1, 6], ['zshDeref', 7, 8]], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 10, 10]], + \ [], + \ [['zshCommands', 1, 6], ['zshDeref', 7, 9], ['zshComment', 11, 11], ['zshTodo', 15, 15], ['zshComment', 19, 57]], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 12, 12]], + \ [], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 15, 15]], + \ [['zshCommands', 1, 6], ['zshDeref', 7, 11], ['zshNumber', 12, 13], ['zshComment', 14, 14], ['zshTodo', 19, 19], ['zshComment', 23, 69]], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 15, 15]], + \ [['zshCommands', 1, 6], ['zshDeref', 7, 11]], + \ [], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 18, 18]], + \ [['zshCommands', 1, 6], ['zshSubstDelim', 7, 7], ['zshSubst', 9, 9], ['zshSubstDelim', 18, 18]], + \ [], + \ [['zshCommands', 1, 6], ['zshShortDeref', 7, 9], ['zshShortDeref', 10, 12], ['zshDeref', 13, 15], ['zshShortDeref', 16, 17]], + \ ]) +endfun diff --git a/testdata/if.zsh b/testdata/if.zsh new file mode 100644 index 0000000..59be09c --- /dev/null +++ b/testdata/if.zsh @@ -0,0 +1,26 @@ +# Test "if" and related constructs in various way. + +if [ $foo ]; then + : +elif [[ $foo ]]; then + : +elif (( $foo )); then + : +fi + +if $(grep x y); do + : +elif [[ $(grep x y) -gt 42 ]]; then + : +elif (( $(grep x y) > 42 )); then + : +fi + +[[ $(grep x y) -gt 42 ]] && print x +(( $(grep x y) > 42 )) && print x + +# Unlike [/[[, (( doesn't need spaces. +[[ x = y ]]&&((1>0))&& print x + +# TODO: maybe show error for these invalid constructs (missing spaces)? +[[x=x]] && [x=x] diff --git a/testdata/quotes.zsh b/testdata/quotes.zsh new file mode 100644 index 0000000..587a12b --- /dev/null +++ b/testdata/quotes.zsh @@ -0,0 +1,13 @@ +# Test quoting. + + 'single' 'var: $var' 'subst: $(ls)' 'esc: \n \x01 \001' + "double" "var: $var" "subst: $(ls)" "esc: \n \x01 \001" # TODO: these last 2 are not highlighted +$'single' $'var: $var' $'subst: $(ls)' $'esc: \n \x01 \001' + `batick` `var: $var` `subst: $(ls)` `esc: \n \x01 \001` + +"'nest'" "`nest`" +'"nest"' '`nest`' + + "escape: \" \' \` \\ escape" + 'escape: \" \' \` \\ escape' # TODO: maybe highlight \' as an error? +$'escape: \" \' \` \\ escape' diff --git a/testdata/var.zsh b/testdata/var.zsh new file mode 100644 index 0000000..52a66f3 --- /dev/null +++ b/testdata/var.zsh @@ -0,0 +1,23 @@ +# Test variables + +# Declaration and assignment. +x=var.zsh +arr=(one two three) +local a=b +typeset -U c=() + +print $x +print ${x} + +print $x:t # TODO: ":t" should probably get highlighted. +print ${x:t} + +print ${arr[1]} +print $arr[1] # TODO: [1] should probably get highlighted the same? +print ${arr[@]} +print $arr[@] + +print ${(j:x:)arr} +print ${(j/x/)arr} + +print $@ $# $_ $0 diff --git a/testing.vim b/testing.vim new file mode 160000 index 0000000..963d2c2 --- /dev/null +++ b/testing.vim @@ -0,0 +1 @@ +Subproject commit 963d2c2488ee85118a9896c06395d68ecbe0f36b From 8d0b50e84acf522f2c3f152d3c48a17890d0ecef Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Fri, 29 Apr 2022 16:39:43 +0200 Subject: [PATCH 2/2] Tweak comments --- testdata/if.zsh | 3 +++ testdata/quotes.zsh | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/testdata/if.zsh b/testdata/if.zsh index 59be09c..bb2081f 100644 --- a/testdata/if.zsh +++ b/testdata/if.zsh @@ -23,4 +23,7 @@ fi [[ x = y ]]&&((1>0))&& print x # TODO: maybe show error for these invalid constructs (missing spaces)? +# Since these are valid glob patterns (though unlikely), we can highlight it as +# an error only if it's after a zshConditional, or if it's preceded or followed +# by zshOperator. [[x=x]] && [x=x] diff --git a/testdata/quotes.zsh b/testdata/quotes.zsh index 587a12b..4353051 100644 --- a/testdata/quotes.zsh +++ b/testdata/quotes.zsh @@ -1,7 +1,7 @@ # Test quoting. 'single' 'var: $var' 'subst: $(ls)' 'esc: \n \x01 \001' - "double" "var: $var" "subst: $(ls)" "esc: \n \x01 \001" # TODO: these last 2 are not highlighted + "double" "var: $var" "subst: $(ls)" "esc: \n \x01 \001" $'single' $'var: $var' $'subst: $(ls)' $'esc: \n \x01 \001' `batick` `var: $var` `subst: $(ls)` `esc: \n \x01 \001` @@ -9,5 +9,5 @@ $'single' $'var: $var' $'subst: $(ls)' $'esc: \n \x01 \001' '"nest"' '`nest`' "escape: \" \' \` \\ escape" - 'escape: \" \' \` \\ escape' # TODO: maybe highlight \' as an error? + 'escape: \" \' \` \\ escape' $'escape: \" \' \` \\ escape'