#! /bin/sh
set -euo pipefail
if ! command -v nearley-test > /dev/null; then
echo >&2 'The command `nearley-test` must be available to run this script. Consider running `npm install -g nearley`.'
exit 2
fi
if ! command -v yq > /dev/null; then
echo >&2 'The command `yq` must be available to run this script.'
exit 2
fi
readonly DATA="${1:-"$(git rev-parse --show-toplevel)/0004-general-uri-scheme/data"}"
readonly TMP="$(mktemp -d)"
readonly RAD_URI_NE="${DATA}/rad-uri.ne"
readonly RAD_URI_JS="${TMP}/rad-uri.js"
if ! nearleyc -o "$RAD_URI_JS" "$RAD_URI_NE"; then
exit 1
fi
FAILED=0
for EXPECTED in "${DATA}"/test/tc*-expected.json; do
INPUT="${EXPECTED%-expected.json}"
NAME="${INPUT##*/}"
ACTUAL="${TMP}/${NAME}.actual"
nearley-test -q -i "$(cat "$INPUT")" "$RAD_URI_JS" | yq > "${ACTUAL}"
if ! diff -u "$EXPECTED" "$ACTUAL" ; then
echo "FAIL: $NAME"
FAILED=1
else
echo "PASS: $NAME"
fi
done
[ "$FAILED" -eq 0 ]