##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.00-init/%M%
#   Revision:		%I%
#   First Edition:	95/12/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Jens Hamisch, Jens.Hamisch@Strawberry.COM
#
#   Description:	Test procedures
#   Command:
#   Sub-Command:
#
#   Comment:	%C{
#			Defines the testprocedures for probing the outputs
#			on stdout and stderr of the test target
#		}C%
#
##############################################################################

#
# some tests only apply if the configure option is enabled or not
#   look at ./modulecmd --version for examples
#   (e.g. version_test("XXX=undef") returns 0 or 1 if the string is found)
# 	if { [ version_test "XXX=1" ] } {
#	} else {
#	}
#
proc version_test {which} {
	global comp_output
	global comp_error
	global verbose

	if { $verbose > 1 } {
		send_user "   ...  version_test($which)\n"
	}
	set comp_output ""
	set comp_error ""

	if { ![info exists which] } {
		unresolved "version_test ($which) internal error"
	}

	modulecmd__ "--version"

	if { ![regexp -- ".*$which.*" $comp_error] } {
		if { $verbose > 1 } {
			send_user "version_test: 0\n"
		}
		return 0
	} else {
		if { $verbose > 1 } {
			send_user "version_test: 1\n"
		}
		return 1
	}
}

#
# Test subprocedures
#

proc _test_sub {test_shell cmd} {
	global comp_output
	global comp_error
	global comp_exit
	global shell
	global verbose

	if { $verbose > 0 } {
		send_user "   ...  Testing 'modulecmd $test_shell $cmd'\n"
	}
	set comp_output ""
	set comp_error ""
	set comp_exit ""

	if { ![info exists test_shell] || ![info exists cmd] } {
		unresolved "$cmd ($test_shell) internal error"
	}

	set shell $test_shell
	modulecmd_start "$cmd"

	if { $verbose > 1 } {
		send_user "OUT: '$comp_output'#>\n"
		send_user "ERR: '$comp_error'#>\n"
		send_user "EXIT: '$comp_exit'#>\n"
	}
}

proc _test_err {test_shell cmd answer} {
	global comp_error
	global verbose

	if { $comp_error != "$answer" } {
		fail "$cmd ($test_shell)"
		send_user "ERR: '$comp_error'#>\n"
		send_user "EXP: '$answer'#>\n"
		return 0;
	} else {
		return 1;
	}
}

proc _test_err_re {test_shell cmd answer} {
	global comp_error
	global verbose

	if { ![regexp -- $answer $comp_error] } {
		fail "$cmd ($test_shell)"
		send_user "ERR: '$comp_error'#>\n"
		send_user "EXP (re): '$answer'#>\n"
		return 0;
	} else {
		return 1;
	}
}

proc _test_out {test_shell cmd answer} {
	global comp_output
	global verbose

	if { $comp_output != "$answer" } {
		fail "$cmd ($test_shell)"
		send_user "OUT: '$comp_output'#>\n"
		send_user "EXP: '$answer'#>\n"
		return 0;
	} else {
		return 1;
	}
}

proc _test_out_re {test_shell cmd answer} {
	global comp_output
	global verbose

	if { ![regexp -- $answer $comp_output] } {
		fail "$cmd ($test_shell)"
		send_user "OUT: '$comp_output'#>\n"
		send_user "EXP (re): '$answer'#>\n"
		return 0;
	} else {
		return 1;
	}
}

proc _test_exit {test_shell cmd exitval} {
	global comp_exit
	global verbose

	if { $comp_exit != "$exitval" } {
		fail "$cmd ($test_shell)"
		send_user "EXIT: '$comp_exit'#>\n"
		send_user "EXP: '$exitval'#>\n"
		return 0;
	} else {
		return 1;
	}
}

proc _test_ok {test_shell cmd} {
	pass "$cmd ($test_shell)"
}

#
# Test procedure for matching with regular expressions
#

proc test_cmd_re {test_shell cmd answer} {
 
	_test_sub $test_shell "$cmd"
	if { [ _test_out_re $test_shell "$cmd" "$answer" ] } {
		_test_ok $test_shell "$cmd"
	}
}

#
#  Test procedure for full text matching
#

proc test_cmd {test_shell cmd answer {exitval 0}} {

	_test_sub $test_shell "$cmd"
	if { [ _test_out $test_shell "$cmd" "$answer" ]
	&&   [ _test_exit $test_shell "$cmd" "$exitval" ] } {
		_test_ok $test_shell "$cmd"
	}
}

#
# Test procedure for matching with regular expressions in the error output
#

proc testerr_cmd_re {test_shell cmd answer} {
    
	_test_sub $test_shell "$cmd"
	if { [ _test_err_re $test_shell "$cmd" "$answer" ] } {
		_test_ok $test_shell "$cmd"
	}
}

#
# Test procedure for matching with regular expressions in the stderr/stdout
#

proc testouterr_cmd_re {test_shell cmd answer anserr} {

	_test_sub $test_shell "$cmd"
	if { [ _test_out_re $test_shell "$cmd" "$answer" ]
	&&   [ _test_err_re $test_shell "$cmd" "$anserr" ] } {
		_test_ok $test_shell "$cmd"
	}
}

#
#  Test procedure for full text matching in the error output
#

proc testerr_cmd {test_shell cmd answer} {

	_test_sub $test_shell "$cmd"
	if { [ _test_err $test_shell "$cmd" "$answer" ] } {
		_test_ok $test_shell "$cmd"
	}
}

#
# Test procedure for full text matching with the stderr/stdout
#

proc testouterr_cmd {test_shell cmd answer anserr} {

	_test_sub $test_shell "$cmd"
	if { [ _test_out $test_shell "$cmd" "$answer" ]
	&&   [ _test_err $test_shell "$cmd" "$anserr" ] } {
		_test_ok $test_shell "$cmd"
	}
}
