#!/bin/sh

OUT=
ERR=
STATUS=

assert_status() {
	if [ "$STATUS" != "$1" ]; then
		printf '\nERR: Bad status.\n\nexpected: %s\ngot:      %s\n' \
			"$1" "$STATUS" >&2
		exit 1
	fi
}

assert_usage() {
  if ! grep -Fq 'Usage' "$1"; then
		echo 'Expected to find "Usage" text, it was missing:' >&2
		cat "$1" >&2
		exit 1
	fi
}

assert_empty_stream() {
	if [ -s "$2" ]; then
		printf '\nERR: Expected %s (%s) to be empty, but has content:\n%s\n' \
			"$1" "$2" "$(cat "$2")" >&2
		exit 1
	fi
}

assert_empty_stdout() {
	assert_empty_stream STDOUT "$OUT"
}

assert_empty_stderr() {
	assert_empty_stream STDERR "$ERR"
}

assert_stream() {
	if [ "$(cat "$2")" != "$3" ]; then
		printf '\nERR: Bad %s (%s)\n\nexpected: %s\ngot:      %s\n' \
			"$1" "$2" "$3" "$(cat "$2")" >&2
		exit 1
	fi
}

assert_stdout() {
	assert_stream STDOUT "$OUT" "$1"
}

assert_stderr() {
	assert_stream STDERR "$ERR" "$1"
}

assert_grep_stream() {
	if ! grep -qE "$3" "$2"; then
		printf '\nERR: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \
			"$1" "$2" "$3"  "$(cat "$2")" >&2
		exit 1
	fi
}

assert_grep_stdout() {
	assert_grep_stream STDOUT "$OUT" "$1"
}

assert_grep_stderr() {
	assert_grep_stream STDERR "$ERR" "$1"
}

testing() {
	printf 'testing: %s...' "$1" >&2
}

test_ok() {
	printf ' OK.\n' >&2
}
