# This file contains support code for the Tcl test suite.  It is
# normally sourced by the individual files in the test suite before
# they run their tests.  This improved approach to testing was designed
# and initially implemented by Mary Ann May-Pumphrey of Sun Microsystems.

# few local changes by dl

if {![info exists VERBOSE]} {set VERBOSE 0}
if {![info exists LOG]} {set LOG 1}

#set auto_noexec 1
#set auto_noload 1
catch {rename unknown ""}

#set user {}
#catch {set user [exec whoami]}

proc print_verbose {test_name test_description contents_of_test code answer} {
    puts stdout "\n"
    puts stdout "==== $test_name $test_description"
    puts stdout "==== Contents of test case:"
    puts stdout "$contents_of_test"
    if {$code != 0} {
        if {$code == 1} {
            puts stdout "==== Test generated error:"
            puts stdout $answer
        } elseif {$code == 2} {
            puts stdout "==== Test generated return exception;  result was:"
            puts stdout $answer
        } elseif {$code == 3} {
            puts stdout "==== Test generated break exception"
        } elseif {$code == 4} {
            puts stdout "==== Test generated continue exception"
        } else {
            puts stdout "==== Test generated exception $code;  message was:"
            puts stdout $answer
        }
    } else {
        puts stdout "==== Result was:"
        puts stdout "$answer"
    }
}

proc test {test_name test_description contents_of_test passing_results} {
    global VERBOSE
    global LOG
    if {$LOG && !$VERBOSE} {
	puts stdout "==== $test_name $test_description"
    }
    set code [catch {uplevel $contents_of_test} answer]
    if {$code != 0} {
        print_verbose $test_name $test_description $contents_of_test \
                $code $answer
    } elseif {[string compare $answer $passing_results] == 0} then {
        if $VERBOSE then {
            print_verbose $test_name $test_description $contents_of_test \
                    $code $answer
            puts stdout "++++ $test_name PASSED"
        }
    } else {
        print_verbose $test_name $test_description $contents_of_test $code \
                $answer
        puts stdout "---- Result should have been:"
        puts stdout "$passing_results"
        puts stdout "---- $test_name FAILED"
    }
}

