-
Notifications
You must be signed in to change notification settings - Fork 148
/
_test_common.sh
executable file
·84 lines (66 loc) · 1.87 KB
/
_test_common.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
function assert_equals {
local arguments=()
for argument in "${@}"
do
arguments+=("${argument}")
done
local assertion_error_file="${PWD}/assertion_error"
for index in ${!arguments[@]}
do
if [ $((index % 2)) -ne 0 ]
then
continue
fi
if [ -f "${arguments[${index}]}" ] &&
[ -f "${arguments[${index} + 1]}" ]
then
local temp_assertion_error_file="${PWD}/temp_assertion_error"
diff \
--side-by-side \
--suppress-common-lines \
"${arguments[${index}]}" "${arguments[${index} + 1]}" > "${temp_assertion_error_file}"
if [ "${?}" -ne 0 ] && [ "${_TEST_RESULT}" == "true" ]
then
_TEST_RESULT="false"
while IFS= read -r line
do
echo "Actual: $(echo "${line}" | cut -d '|' -f 1 | xargs)" >> "${assertion_error_file}"
echo -e "Expected: $(echo "${line}" | cut -d '|' -f 2 | xargs)\n" >> "${assertion_error_file}"
done < "${temp_assertion_error_file}"
fi
rm -f "${temp_assertion_error_file}"
else
if [ "${arguments[${index}]}" != "${arguments[${index} + 1]}" ]
then
if [ "${_TEST_RESULT}" == "true" ]
then
_TEST_RESULT="false"
fi
touch "${assertion_error_file}"
echo "Actual: ${arguments[${index}]}" >> "${assertion_error_file}"
echo -e "Expected: ${arguments[${index} + 1]}\n" >> "${assertion_error_file}"
fi
fi
done
if [ "${_TEST_RESULT}" == "true" ]
then
echo -e "${FUNCNAME[1]} \e[1;32mSUCCESS\e[0m\n"
else
echo -e "${FUNCNAME[1]} \e[1;31mFAILED\e[0m\n"
cat "${assertion_error_file}"
rm -f "${assertion_error_file}"
_TEST_RESULT="true"
fi
}
function main {
_TEST_RESULT="true"
if [ -n "${BASH_SOURCE[3]}" ]
then
echo -e "\n##### Running tests from $(echo ${BASH_SOURCE[3]} | sed -r 's/\.\///g') #####\n"
elif [ -n "${BASH_SOURCE[2]}" ]
then
echo -e "\n##### Running tests from $(echo ${BASH_SOURCE[2]} | sed -r 's/\.\///g') #####\n"
fi
}
main