#!@bash@

SRCDIR=$(pwd)/../src
cd `dirname $0`
FILE=`basename $0 .sh`
bash @prefix@/bin/webdsl cleanall > /dev/null 2> /dev/null
bash @prefix@/bin/webdsl test-ng $FILE 2>&1 | cat > $FILE.out

exec 3<> $FILE.app
while read line <&3
do
if echo "$line" | grep -q "^//"; then
pattern=`echo "$line" | sed 's/^[/]*//'`
echo $pattern | grep -q "^\^"
negate=$?
echo $pattern | grep -q "^#"
count=$?
if [[ $negate == 0 ]]; then
echo "error must not be shown"
if grep -q "${pattern:1}" $FILE.out; then
echo "Error should not be present: ${pattern:1}"
is_error=1
fi
elif [[ $count == 0 ]]; then
echo "error must be shown X times"
occurrences=`echo $pattern | perl -pe 's/^#([0-9]+) .*$/\1/'`
echo "X = $occurrences"
pattern=`echo $pattern | perl -pe 's/^#[0-9]+ (.*)$/\1/'`
echo "error = $pattern"
foundlines=`grep "$pattern" $FILE.out | wc -l`
echo "found X = $foundlines"
if [ $foundlines -ne $occurrences ]; then
echo "Error should be reported $occurrences times: $pattern"
is_error=1
fi
else
echo "error must be shown"
if ! grep -q "$pattern" $FILE.out; then
echo "Error should be present: $pattern"
is_error=1
fi
fi
else
break
fi
done
exec 3>&-

if [[ $is_error ]]; then
echo "------------------------------------"
echo
cat $FILE.out
exit 1
fi

exit 0