#!/bin/sh
set -e
SNOWBALL='../snowball -o tmp'
r=0

# Tests for expected compiler errors.
set x errors/*.sbl
shift
echo "1..$#"
for t ; do
  b=`echo "$t"|sed 's/\.sbl$//'`
  f=
  # Run with -syntax to avoid generating files if we get none of the expected
  # errors.
  if $SNOWBALL -syntax "$t" 2> tmp.stderr > tmp.syntax ; then
    f="snowball compiler did not fail"
  else
    if [ -f "$b.stderr" ] ; then
      if ! diff "$b.stderr" tmp.stderr ; then
	f="stderr output not as expected"
      fi
    fi
  fi
  if [ -z "$f" ] ; then
    echo "ok - $b"
  else
    echo "not ok - $b: $f"
    r=1
  fi
done

# Tests which check --syntax output.
set x syntax/*.sbl
shift
echo "1..$#"
for t ; do
  b=`echo "$t"|sed 's/\.sbl$//'`
  f=
  if $SNOWBALL -syntax "$t" 2> tmp.stderr > tmp.syntax ; then
    if [ -f "$b.stderr" ] ; then
      if ! diff "$b.stderr" tmp.stderr ; then
	f="stderr output not as expected"
      fi
    fi
    if [ -z "$f" ] && [ -f "$b.syntax" ] ; then
      if ! diff "$b.syntax" tmp.syntax ; then
	f="syntax tree not as expected"
      else
	rm tmp.syntax
      fi
    fi
  else
    f="snowball compiler failed"
  fi
  if [ -z "$f" ] ; then
    echo "ok - $b"
    rm -f tmp.stderr tmp.syntax
  else
    echo "not ok - $b: $f"
    r=1
  fi
done

exit $r
