#!/bin/bash
#  check - System Testing Support

set -e

lib_load 'core/math'


######
# Library Configuration

sys_check_config_init() {
	# $check_list[] - Stack of checks being performed.
	lib_setting_arrays check_list
}

######
# Internal Functions

# check_start() - Prints message indicating a check ($1) has started
check_start() {
	has_args 1 "$@";
	int_inc check_count_start
	app_echo_n "$check_list: $1"
}

# check_finish() - Prints message indicating a check ($1) has finished.
check_finish() {
	has_args 1 "$@";
	int_inc check_count_finish
	echo "$1"
}

# check_pass() - Prints a message indicating a check passed.
check_pass() { int_inc check_count_pass; check_finish "PASS"; }

# check_fail() - Prints a message ($1) indicating a check failed.
check_fail() { int_inc check_count_fail; check_finish "FAIL"; echo "$1"; }


######
# Checks

# check_run() - Runs a single check ($@) with a given message ($1)
# and error message ($2).
# $1 - Informational message to pass to ``check_start()``.
# $2 - Error message to pass to ``check_fail()``.
# $@ - Command to execute.
check_run() {
	min_args 2 "$@"
	local _msg="$1"
	local _err="$2"
	shift 2
	check_start "$_msg: "
	run "$@" && check_pass || check_fail "$_err"
}


######
# Tests

# check_for_pass() - Runs a check that is expected to pass.
# $1 - Informational message to pass to ``check_start()``.
# $@ - Command to execute.
check_for_pass() {
	local _msg=$1
	shift
	check_run "$_msg" "FAIL" "$@"
}

# check_for_fail() - Runs a check that is expected to fail.
# $1 - Informational message to pass to ``check_start()``.
# $@ - Command to execute.
check_for_fail() {
	local _msg=$1
	shift
	check_start "$_msg: "
	local -a _args=( "$@" )
	local out=$(check_run "$_msg" "FAIL" "${_args[@]}" 2>&1)
	(echo "$out" | grep FAIL >/dev/null) && check_pass || check_fail "$out"
}


# check_is_expected() - Runs a check that verifies a given variable ($1)
# produce an expected value ($2).
# $1 - Variable to check
# $2 - Expected value
check_is_expected() {
	has_args 2 "$@"
	local _var=$1
	local _expected=$2

	local _value
	eval "_value=\"\$$_var\""
	local _msg="$_var: '$_value' == '$_expected'"
	check_run "$_msg" 'values do not match' [ "$_value" = "$_expected" ]
}

######
# Test Suites

# check_list_start() - Starts a test suite.
# $1 - The name of the testsuite.  If `null`, the caller function name
# well be used.  The name must be a valid symbol.
check_list_start() {
	local fname=${1:-${FUNCNAME[1]}}
	list_insert check_list $fname
	check_count_start=0
	check_count_finish=0
	check_count_pass=0
	check_count_fail=0
	app_echo "$fname: Starting..."
}

# check_list_finish() - Finishs a test suite and reports the results.
check_list_finish() {
	local fname
	fname=$(list_fpop check_list)
	local msg
	msg=$(printf "%s: ... finished: %d/%d passed (%d/%d finished)" \
		"$fname" "$(($check_count_pass - $check_count_fail))" "$check_count_pass" \
		"$check_count_finish" "$check_count_start")
	app_echo "$msg"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:58 PDT 2017 by mcsh d14 v0.23.0.