osync/dev/tests/shunit2/shunit2_macros_test.sh

248 lines
6.7 KiB
Bash
Raw Permalink Normal View History

2016-08-22 08:26:38 +00:00
#! /bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
2018-07-02 21:25:49 +00:00
# shunit2 unit test for macros.
#
2022-07-07 14:22:35 +00:00
# Copyright 2008-2021 Kate Ward. All Rights Reserved.
2018-07-02 21:25:49 +00:00
# Released under the Apache 2.0 license.
2022-07-07 14:22:35 +00:00
# http://www.apache.org/licenses/LICENSE-2.0
2018-07-02 21:25:49 +00:00
#
2016-08-22 08:26:38 +00:00
# Author: kate.ward@forestent.com (Kate Ward)
2018-07-02 21:25:49 +00:00
# https://github.com/kward/shunit2
2016-08-22 08:26:38 +00:00
#
2018-07-02 21:25:49 +00:00
# Disable source following.
# shellcheck disable=SC1090,SC1091
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
# These variables will be overridden by the test helpers.
stdoutF="${TMPDIR:-/tmp}/STDOUT"
stderrF="${TMPDIR:-/tmp}/STDERR"
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
# Load test helpers.
. ./shunit2_test_helpers
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
testAssertEquals() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_EQUALS_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_EQUALS_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_EQUALS_} '"some msg"' 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_EQUALS_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testAssertNotEquals() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_EQUALS_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_EQUALS_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_EQUALS_} '"some msg"' 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_EQUALS_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testSame() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_SAME_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_SAME_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_SAME_} '"some msg"' 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_SAME_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testNotSame() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_SAME_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_SAME_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_SAME_} '"some msg"' 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_SAME_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testNull() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NULL_} 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NULL_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NULL_} '"some msg"' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NULL_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2022-07-07 14:22:35 +00:00
testNotNull() {
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_NULL_} '' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_NULL_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_ASSERT_NOT_NULL_} '"some msg"' '""' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_NOT_NULL_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testAssertTrue() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
( ${_ASSERT_TRUE_} "${SHUNIT_FALSE}" >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_TRUE_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
( ${_ASSERT_TRUE_} '"some msg"' "${SHUNIT_FALSE}" >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_TRUE_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testAssertFalse() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
( ${_ASSERT_FALSE_} "${SHUNIT_TRUE}" >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_FALSE_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
2018-07-02 21:25:49 +00:00
( ${_ASSERT_FALSE_} '"some msg"' "${SHUNIT_TRUE}" >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_ASSERT_FALSE_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testFail() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_FAIL_} >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_FAIL_} '"some msg"' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2022-07-07 14:22:35 +00:00
testFailNotEquals() {
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_FAIL_NOT_EQUALS_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_NOT_EQUALS_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_FAIL_NOT_EQUALS_} '"some msg"' 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_NOT_EQUALS_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testFailSame() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_FAIL_SAME_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_SAME_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_FAIL_SAME_} '"some msg"' 'x' 'x' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_SAME_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
testFailNotSame() {
2022-07-07 14:22:35 +00:00
isLinenoWorking || startSkipping
2016-08-22 08:26:38 +00:00
( ${_FAIL_NOT_SAME_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_NOT_SAME_ failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
( ${_FAIL_NOT_SAME_} '"some msg"' 'x' 'y' >"${stdoutF}" 2>"${stderrF}" )
2022-07-07 14:22:35 +00:00
if ! wasAssertGenerated; then
fail '_FAIL_NOT_SAME_ (with a message) failed to produce an ASSERT message'
showTestOutput
fi
2016-08-22 08:26:38 +00:00
}
2018-07-02 21:25:49 +00:00
oneTimeSetUp() {
2016-08-22 08:26:38 +00:00
th_oneTimeSetUp
2022-07-07 14:22:35 +00:00
if ! isLinenoWorking; then
# shellcheck disable=SC2016
th_warn '${LINENO} is not working for this shell. Tests will be skipped.'
fi
}
# isLinenoWorking returns true if the `$LINENO` shell variable works properly.
isLinenoWorking() {
# shellcheck disable=SC2016
ln='eval echo "${LINENO:-}"'
case ${ln} in
[0-9]*) return "${SHUNIT_TRUE}" ;;
-[0-9]*) return "${SHUNIT_FALSE}" ;; # The dash shell produces negative values.
esac
return "${SHUNIT_FALSE}"
2016-08-22 08:26:38 +00:00
}
2022-07-07 14:22:35 +00:00
# showTestOutput for the most recently run test.
showTestOutput() { th_showOutput "${SHUNIT_FALSE}" "${stdoutF}" "${stderrF}"; }
# wasAssertGenerated returns true if an ASSERT was generated to STDOUT.
wasAssertGenerated() { grep '^ASSERT:\[[0-9]*\] *' "${stdoutF}" >/dev/null; }
2018-07-02 21:25:49 +00:00
# Disable output coloring as it breaks the tests.
SHUNIT_COLOR='none'; export SHUNIT_COLOR
# Load and run shUnit2.
# shellcheck disable=SC2034
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT="$0"
. "${TH_SHUNIT}"