################################################################################
# shUnit 
# original by Vera Peeters & Rik Tytgat
# $Source: /cvsroot/shunit/ShUnit/shUnit,v $
# $Id: shUnit,v 1.13 2005/12/31 06:03:32 bobbynations Exp $
################################################################################
#
# Note that this is a very basic implementation of the Unit Framework: no Suites
# For more information about Unit Testing Frameworks: see http://www.xprogramming.com.
#
# Questions or request concerning shUnit:
# see http://sourceforge.net/projects/shunit
#
# How to use shUnit
# -----------------
#
# See the shUnitTest file for a simple straightforward example of how to
# use the shUnit Unit Test Framework.
#
# What you need to do to create your own shUnit Tests:
# 
# 1. Write a script.
# 2. In the 'main' of the script, the shUnit functions have to be
#    sourced in ( . /wherever/the/shUnit/file/is/shUnit)
# 3. Create a function in your test scripts that prepares the environment
#    in which your test will be run
# 4. Again in the 'main', call the shuStart function with the name of the initialization
#    function as an argument.
# 5. For each testfunction, perform these steps:
#    5.1. Add a line to the initialization function to register the testfunction. This
#         will look like: shuRegTest NameOfTestFunction
#    5.2. Create a function NameOfTestFuction that
#           5.2.1. executes whatever you wish to test
#           5.2.2. Checks the result of what you've just tested
#           5.2.3. Calls the shuAssert function to assert that the result is as expected
#           5.2.4. You can put as many asserts as you wish in each testfunction.
# 6. Run your testscript
#
# You can of course repeat step 5 as much as you like (many testfunctions).
#

# Function Declarations

shuRegTest() {
  strFunction=${1}

  SHU_STR_ALL_TESTS="${SHU_STR_ALL_TESTS} ${strFunction}"
}

shuRunOneTest() {
  strTestToRun=${1}
  SHU_TEST_SUCCEEDED=${SHU_FALSE}

  if type shuSetUp > /dev/null 2>&1
  then
      shuSetUp
  fi

  if type ${strTestToRun} > /dev/null 2>&1
  then
    ${strTestToRun}
    if [ ${SHU_TEST_SUCCEEDED} -eq ${SHU_TRUE} ]
    then
      SHU_TOTAL_NR_SUCCEEDED=`expr $SHU_TOTAL_NR_SUCCEEDED + 1`
    fi
  else
    SHU_TEST_SUCCEEDED=${SHU_FALSE}
    printf "  ERROR: \"%s\" not found.\n" "${strTestToRun}"
  fi

  if type shuTearDown > /dev/null 2>&1
  then
      shuTearDown
  fi

}

shuFmtNbrTests() {
    n="${1}"
    if [ ${n} -eq 0 ] 
    then
        shuRetFmtNbrTests="No tests"
    elif [ ${n} -eq 1 ] 
    then
        shuRetFmtNbrTests="1 test" 
    else
        shuRetFmtNbrTests="${n} tests" 
    fi
}

shuStart() {
  strInitFunction="${1}"

  eval ${strInitFunction}

  printf "\n****** `basename ${0}` ******\n"

  SHU_TOTAL_NR_OF_TESTS=`echo ${SHU_STR_ALL_TESTS} | wc -w | sed -e 's/ *//'` 
  shuFmtNbrTests "${SHU_TOTAL_NR_OF_TESTS}"
  printf "%s to run:\n" "$shuRetFmtNbrTests"

  SHU_TOTAL_NR_SUCCEEDED=0
  shuTestNbr=0
  for STR_TEST in ${SHU_STR_ALL_TESTS}
  do
    SHU_STR_FAILED=""
    shuTestNbr=`expr ${shuTestNbr} + 1`
    printf "    Test %i: %s    " ${shuTestNbr} "${STR_TEST}"
    shuRunOneTest ${STR_TEST}
    printf "\n"

    if test -n "${SHU_STR_FAILED}"
    then 

      OLD_IFS="${IFS}"
      IFS="^"
      set -- ${SHU_STR_FAILED}
      IFS="${OLD_IFS}"
      while [ ${#} -gt 0 ]
      do
        printf "      \"%s\" failed.\n" "${1}" 
        shift 
      done
    fi
  done

  shuFmtNbrTests "${shuTestNbr}"
  printf "%s run.\n" "${shuRetFmtNbrTests}"

  shuFmtNbrTests "${SHU_TOTAL_NR_SUCCEEDED}"
  printf "%s succeeded.\n\n" "${shuRetFmtNbrTests}"
}

shuRegisterFailedTest() {
    msg="${1}"
    if [ -z "${SHU_STR_FAILED}" ]
    then
        SHU_STR_FAILED="${msg}"
    else
        SHU_STR_FAILED="${SHU_STR_FAILED}^${msg}"
    fi
    SHU_TEST_SUCCEEDED=${SHU_FALSE}
}

shuAssert() {
  strMessage="${1:-}"
  boolResult="${2:-${SHU_FALSE}}"

  if [ ${boolResult} -ne ${SHU_TRUE} ]
  then
    printf "E"
    shuRegisterFailedTest "${strMessage}"
  else
    printf "."
    SHU_TEST_SUCCEEDED=${SHU_TRUE}
  fi
}

shuDeny() {
  strMessage="${1:-}"
  boolResult="${2:-${SHU_TRUE}}"

  if [ ${boolResult} -eq ${SHU_TRUE} ]
  then
    printf "E"
    shuRegisterFailedTest "${strMessage}"
  else
    printf "."
    SHU_TEST_SUCCEEDED=${SHU_TRUE}
  fi
}

# Global Variables
SHU_TRUE=0
SHU_FALSE=1
