dev tools: Add a script that generates a test-data file.

This commit is contained in:
Daniel Shahaf 2016-07-28 23:15:32 +00:00
parent fa57633d81
commit 8013dc3b8d
2 changed files with 102 additions and 0 deletions

View File

@ -39,6 +39,25 @@ which is automatically cleaned up after the test exits. For example:
"1 21 command" # bar/testing-issue-228
)
Writing new tests
-----------------
An experimental tool is available to generate test files:
zsh -f tests/generate.zsh 'ls -x' \
| sed s/YYYY/$(date +%Y)/ \
> highlighters/main/test-data/foo.zsh
git add -N $_
This generates a test file based on the current highlighting of the given `$BUFFER`
(in this case, `ls -x`).
_This tool is experimental._ Its interface may change. In particular it may
grow ways to set `$PREBUFFER` and/or `$ZSH_HIGHLIGHT_HIGHLIGHTERS` or to
inject free-form code into the generated file.
Highlighting test
-----------------

83
tests/generate.zsh Executable file
View File

@ -0,0 +1,83 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2016 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
emulate -LR zsh
# Argument parsing.
if (( $# != 1 )) || [[ $1 == -* ]]; then
print -r -- >&2 "$0: usage: $0 BUFFER"
print -r -- >&2 ""
print -r -- >&2 "This tool generates a test file, suitable for highlighters/*/test-data/."
exit 1
fi
buffer=$1
# Load the main script.
. ${0:A:h:h}/zsh-syntax-highlighting.zsh
# Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
_zsh_highlight_add_highlight()
{
region_highlight+=("$1 $2 $3")
}
# Copyright block
<$0 sed -n -e '1,/^$/p' | sed -e 's/2[0-9][0-9][0-9]/YYYY/'
echo ""
# Buffer
print -n 'BUFFER='
print -r -- ${(qq)buffer}
echo ""
# Expectations
print 'expected_region_highlight=('
() {
local i
local PREBUFFER
local BUFFER
PREBUFFER=""
BUFFER="$buffer"
region_highlight=()
_zsh_highlight
for ((i=1; i<=${#region_highlight}; i++)); do
local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} )
integer start=$highlight_zone[1] end=$highlight_zone[2]
if (( start < end )) # region_highlight ranges are half-open
then
(( --end )) # convert to closed range, like expected_region_highlight
(( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed
fi
printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} $BUFFER[start,end]
done
}
print ')'