#! /bin/sh # vim:et:ft=sh:sts=2:sw=2 # # shFlags unit tests for the internal functions. # # Copyright 2008-2020 Kate Ward. All Rights Reserved. # Released under the Apache 2.0 license. # # Author: kate.ward@forestent.com (Kate Ward) # https://github.com/kward/shflags # ### ShellCheck (http://www.shellcheck.net/) # Disable source following. # shellcheck disable=SC1090,SC1091 # expr may be antiquated, but it is the only solution in some cases. # shellcheck disable=SC2003 # $() are not fully portable (POSIX != portable). # shellcheck disable=SC2006 # These variables will be overridden by the test helpers. stdoutF="${TMPDIR:-/tmp}/STDOUT" stderrF="${TMPDIR:-/tmp}/STDERR" # Load test helpers. . ./shflags_test_helpers testColumns() { cols=`_flags_columns` value=`expr "${cols}" : '\([0-9]*\)'` assertNotNull "unexpected screen width (${cols})" "${value}" } testGetoptVers() { # shellcheck disable=SC2162 while read desc mock want; do assertEquals "${desc}" "$(_flags_getopt_vers "${mock}")" "${want}" done <"${stderrF}"`"; then fail "${desc}: expected invalid flag request to fail" th_showOutput else assertEquals "${desc}: expected an error" "${FLAGS_ERROR}" $? assertErrorMsg "missing flag info variable" fi } testItemInList() { list='this is a test' # shellcheck disable=SC2162 while read desc item want; do if [ "${want}" -eq "${FLAGS_TRUE}" ]; then continue fi got=${FLAGS_TRUE} if ! _flags_itemInList "${item}" "${list}"; then got=${FLAGS_FALSE} fi assertEquals "${desc}: itemInList(${item})" "${want}" "${got}" done </dev/null 2>&1; then got=${FLAGS_FALSE} fi assertFalse 'missing math succeeded' "${got}" } testMathBuiltin() { _flags_useBuiltin || startSkipping _testMath } testMathExpr() { ( _flags_useBuiltin() { return "${FLAGS_FALSE}"; } _testMath ) } _testStrlen() { len=`_flags_strlen` assertTrue 'missing argument failed' $? assertEquals 'missing argument' 0 "${len}" len=`_flags_strlen ''` assertTrue 'empty argument failed' $? assertEquals 'empty argument' 0 "${len}" len=`_flags_strlen abc123` assertTrue 'single-word failed' $? assertEquals 'single-word' 6 "${len}" len=`_flags_strlen 'This is a test'` assertTrue 'multi-word failed' $? assertEquals 'multi-word' 14 "${len}" } testStrlenBuiltin() { _flags_useBuiltin || startSkipping _testStrlen } testStrlenExpr() { ( _flags_useBuiltin() { return "${FLAGS_FALSE}"; } _testStrlen ) } oneTimeSetUp() { th_oneTimeSetUp _flags_useBuiltin || \ th_warn 'Shell built-ins not supported. Some tests will be skipped.' } tearDown() { flags_reset } # Load and run shUnit2. # shellcheck disable=SC2034 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0 . "${TH_SHUNIT}"