🚀.
[pin4sha_cgi.git] / scripts / assert.sh
blob5d757fa1c145948e9adba10aa7d93ce5170d90dc
2 # Copyright (c) 2015 Marcus Rohrmoser http://mro.name/me. All rights reserved.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # insipired by https://github.com/lehmannro/assert.sh but much more primitive.
20 # terminal colors (require bash)
21 # http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
22 # http://wiki.bash-hackers.org/scripting/terminalcodes
23 FGC_NONE="\033[0m"
24 FGC_GRAY="\033[1;30m"
25 FGC_RED="\033[1;31m"
26 FGC_GREEN="\033[1;32m"
27 FGC_YELLOW="\033[1;33m"
28 FGC_BLUE="\033[1;34m"
29 FGC_PURPLE="\033[1;35m"
30 FGC_CYAN="\033[1;36m"
31 FGC_WHITE="\033[1;37m"
32 BGC_GRAY="\033[7;30m"
33 BGC_RED="\033[7;31m"
34 BGC_GREEN="\033[7;32m"
35 BGC_YELLOW="\033[7;33m"
36 BGC_BLUE="\033[7;34m"
37 BGC_PURPLE="\033[7;35m"
38 BGC_CYAN="\033[7;36m"
39 BGC_WHITE="\033[7;37m"
41 assert_fail() {
42 local CODE="${1}" MESSAGE="${2}"
43 echo -e "${BGC_RED}assert_fail (${CODE}): ${MESSAGE}${FGC_NONE}"
44 exit $1
47 assert_equal() {
48 local EXPECTED="${1}" ACTUAL="${2}" CODE="${3}" MESSAGE="${4}"
49 if [ ! "${EXPECTED}" = "${ACTUAL}" ] ; then
50 echo -e "${BGC_RED}assert_equal${FGC_NONE} (${CODE}): ${MESSAGE} expected: \"${FGC_GREEN}${EXPECTED}${FGC_NONE}\" != actual: \"${FGC_RED}${ACTUAL}${FGC_NONE}\""
51 exit $3
55 assert_fgrep() {
56 local PATTERN="${1}" ACTUAL="${2}" CODE="${3}" MESSAGE="${4}"
57 echo "${ACTUAL}" | fgrep "${PATTERN}" 1> /dev/null 2>&1 || { \
58 echo -e "${BGC_RED}assert_equal${FGC_NONE} (${CODE}): ${MESSAGE} pattern: \"${FGC_GREEN}${PATTERN}${FGC_NONE}\" != actual: \"${FGC_RED}${ACTUAL}${FGC_NONE}\"" \
59 && exit $3 ; \