-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathassert.sh
65 lines (55 loc) · 1.08 KB
/
assert.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# Copied from https://github.com/orangemug/bash-assert/blob/28a08c136196bd97d9e3724400aa9a07cd0e7da7/assert.sh
# License: The MIT License (MIT)
# Copyright (c) 2015 Jamie Blair
__assert ()
{
E_PARAM_ERR=98
E_ASSERT_FAILED=99
lineno=`caller 1`
if [[ $# < 2 || $# > 4 ]]
then
num=`expr $# - 1`
>&2 echo "ERR: assert require 1, 2 or 3 params, got $num"
return $E_PARAM_ERR
fi
if [ $# -eq 2 ]; then
cmd="check exit code: $?"
if [ "$?" -eq 0 ]
then
success="true"
else
success="false"
fi
elif [ $# -eq 3 ]; then
cmd="\"$2\" -eq \"$4\""
if [ "$2" "$3" ]
then
success="true"
else
success="false"
fi
else
cmd="\"$2\" $3 \"$4\""
if [ "$2" "$3" "$4" ]
then
success="true"
else
success="false"
fi
fi
if [ "$success" != "$1" ]
then
>&2 echo "Assertion failed: \"$cmd\""
>&2 echo "File \"$0\", line $lineno"
return $E_ASSERT_FAILED
fi
}
assert() {
__assert "true" "$@";
return $?
}
assert_fail() {
__assert "false" "$@";
return $?
}