#!/bin/sh

#
# For each command-line argument, run the corresponding test as follows:
#
# - Load the expected result $testname.res and save it in $testname.tmp1
#
# - Run $testname.cmd and save the actual result in $testname.tmp2
#
# - Check that there are no differences between the actual and the
#   expected results. If there are, the resulting $testname.diff and
#   and $testname.log files are kept.
#

if [ -z "$*" ]; then
	set -- *.cmd
fi

echo 1..$#

#
# loop over all tests
#
for i; do
	i=${i%.cmd}
	(echo	load \"$i.res\"\;				\
		save \"$i.tmp1\"\;				\
		reset \;					\
		exec \"$i.cmd\"\;				\
		save \"$i.tmp2\"\;				\
			| ../midish -b >$i.log 2>&1 )		\
	&&							\
	diff -u $i.tmp1 $i.tmp2 >$i.diff 2>>$i.log
	if [ "$?" -eq 0 ]; then
		echo ok $i
		rm -- $i.tmp1 $i.tmp2 $i.diff $i.log
	else
		echo not ok $i
		failed="$failed $i"
	fi
done

#
# print summary, and set exit code
#
echo >&2
if [ -n "$failed" ]; then
	echo Tests failed: $failed >&2
	exit 1
else
	echo Tests passed
	exit 0
fi
