• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# vim:et:ft=sh:sts=2:sw=2
2#
3# shFlags unit test common functions
4
5__th_skipping=0
6
7# treat unset variables as an error
8set -u
9
10# set shwordsplit for zsh
11[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
12
13# my name
14TH_MY_NAME=`basename "$0"`
15
16# path to shFlags library. can be overridden by setting SHFLAGS_INC
17TH_SHFLAGS=${SHFLAGS_INC:-./shflags}
18
19# path to shUnit2 library. can be overridden by setting SHUNIT_INC
20TH_SHUNIT=${SHUNIT_INC:-../lib/shunit2}
21
22TH_BOOL_VALID='true t 0 false f 1'
23TH_BOOL_INVALID='123 123.0 invalid'
24TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0'
25TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""'
26TH_INT_VALID='-1234 -1 0 1 1234'
27TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""'
28
29#
30# test helper functions
31#
32
33# message functions
34th_trace() { echo "test:TRACE $@" >&2; }
35th_debug() { echo "test:DEBUG $@" >&2; }
36th_info() { echo "test:INFO $@" >&2; }
37th_warn() { echo "test:WARN $@" >&2; }
38th_error() { echo "test:ERROR $@" >&2; }
39th_fatal() { echo "test:FATAL $@" >&2; }
40
41th_oneTimeSetUp()
42{
43  # load shFlags
44  [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0
45  . ${TH_SHFLAGS}
46
47  # these files will be cleaned up automatically by shUnit2
48  tmpDir=${SHUNIT_TMPDIR}
49  stdoutF="${tmpDir}/stdout"
50  stderrF="${tmpDir}/stderr"
51  returnF="${tmpDir}/return"
52  expectedF="${tmpDir}/expected"
53}
54
55th_showOutput()
56{
57  _th_rtrn=$1
58  _th_stdout=$2
59  _th_stderr=$3
60
61  isSkipping
62  if [ $? -eq ${SHUNIT_FALSE} -a ${_th_rtrn} != ${FLAGS_TRUE} ]; then
63    if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then
64      echo '>>> STDOUT' >&2
65      cat "${_th_stdout}" >&2
66    fi
67    if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then
68      echo '>>> STDERR' >&2
69      cat "${_th_stderr}" >&2
70    fi
71    if [ -n "${_th_stdout}" -o -n "${_th_stderr}" ]; then
72      echo '<<< end output' >&2
73    fi
74  fi
75
76  unset _th_rtrn _th_stdout _th_stderr
77}
78
79# Some shells, zsh on Solaris in particular, return immediately from a sub-shell
80# when a non-zero return value is encountered. To properly catch these values,
81# they are either written to disk, or recognized as an error the file is empty.
82th_clearReturn() { cp /dev/null "${returnF}"; }
83th_queryReturn()
84{
85  if [ -s "${returnF}" ]; then
86    th_return=`cat "${returnF}"`
87  else
88    th_return=${SHUNIT_ERROR}
89  fi
90}
91
92_th_assertMsg()
93{
94  _th_alert_type_=$1
95  _th_alert_msg_=$2
96  _th_msg_=$3
97
98  case ${_th_alert_type_} in
99    WARN) _th_alert_str_='a warning' ;;
100    ERROR) _th_alert_str_='an error' ;;
101    FATAL) _th_alert_str_='a fatal' ;;
102  esac
103  [ -z "${_th_alert_msg_}" ] && _th_alert_msg_='.*'
104  [ -n "${_th_msg_}" ] && _th_msg_="(${_th_msg_}) "
105
106  grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" \
107      >/dev/null
108  assertTrue \
109      "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $?
110
111  unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_
112}
113
114assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
115assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
116assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }
117