Skip to content

Add a few tests #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "testing.vim"]
path = testing.vim
url = [email protected]:arp242/testing.vim.git
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
81 changes: 81 additions & 0 deletions syntax_test.vim
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions testdata/if.zsh
Original file line number Diff line number Diff line change
@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't invalid constructs; they're perfectly valid glob patterns. The first looks for a file named [] or =] or x]. The second one looks for a file named x or =.

Nevertheless, I agree that it might make sense to highlight these constructs as "this is probably wrong" (compare zsh-users/zsh-syntax-highlighting#695 and the issue linked from the first post there).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, of course. I think we can get around that by highlighting it as an error only if it's after a zshConditional, or if it's preceded or followed by zshOperator. I'll update the comment.

13 changes: 13 additions & 0 deletions testdata/quotes.zsh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine, without this PR or other recent changes:

  • The $( and ) inside double quotes are highlighted, contrary to what the comment says.

  • The \n \x01 \000 are highlighted, contrary to what the comment says… but they shouldn't be:

    % print -r -- "\n \x01 \001" 
    \n \x01 \001

$'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'
Copy link
Contributor

@danielshahaf danielshahaf Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful here. Since \' is perfectly valid syntax, this line has two single-quoted strings: one with contents escape: \" \ and another with contents # TODO: maybe highlight \ [github is eating the leading whitespace]. There isn't any comment on this line at all, either.

Thus, if someone thinks that's a comment and adds the word "don't" to it, line 13 would no longer have a $'…' string in it. That seems somewhat brittle.

23 changes: 23 additions & 0 deletions testdata/var.zsh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions testing.vim
Submodule testing.vim added at 963d2c