#!/bin/bash
#  check - testing support

set -e

lib_load 'core/math'

######
# Configuration

lib_setting_arrays check_list

######
# Internal functions

check_start() {
	has_args 1 "$@";
	int_inc check_count_start
	app_echo_n "$check_list: $1"
}
check_finish() {
	has_args 1 "$@";
	int_inc check_count_finish
	echo "$1"
}
check_pass() { int_inc check_count_pass; check_finish "PASS"; }
check_fail() { int_inc check_count_fail; check_finish "FAIL"; echo "$1"; }

check_run() {
	min_args 2 "$@"
	local _msg="$1"
	local _err="$2"
	shift 2
	check_start "$_msg: "
	run "$@" && check_pass || check_fail "$_err"
}

######
# Test support

check_for_pass() {
	local _msg=$1
	shift
	check_run "$_msg" "FAIL" "$@"
}

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() {
	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" ]
}

######
# Testsuite support

check_list_start() {
	local fname=${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() {
	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 Script Reference Index


Generated on Tue Apr 25 21:21:14 PDT 2017 by mcsh i7 v0.18.0.