summaryrefslogtreecommitdiff
path: root/src/util/shtests.sh
blob: 44ad48efe366d818840fd539d5703165a8d2b7ae (plain)
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
#!/bin/sh
#
# Simple shell test suite
#
# will run all functions with a name starting with test_ 
# the return value of said function will determine if its a pass or fail
#
# to run a shell file full of unit tests, run:
#   shtests [FILE]
#

#include colors

PASS="${BLUE}[ ${GREEN}PASS${BLUE} ]${RESET}"
FAIL="${BLUE}[ ${RED}FAIL${BLUE} ]${RESET}"

V=false

runtest () {
    test_name=$(echo $1 | sed "s/_/ /g")
    test_func="$2"
    printf "${BLUE}[      ] ${RESET}$test_name ";
    if "$test_func" ; then
        printf "\r$PASS\n"
        return 0
    else
        printf "\r$FAIL\n"
        return 1
    fi
}

# TODO use getopt for this
if [ "$1" = "-v" ]; then
    shift
    V=true
fi

if [ $# = "0" ]; then
    printf "${RED}No tests file has been provided\n"
    exit 1;
else
    . $@
fi

tests=$(sed -n "s/^test_\(.*\)\s*()\s*{/\1/p" $@)
total=$(echo $tests | wc -w)
passed=0
failed=0

printf "${BLUE}Running $total tests: \n"
start=$(date +%s%N)
for name in $tests; do
    if runtest "$name" "test_$name"; then
        passed=$((passed+1))
    else
        failed=$((failed+1))
    fi
done
end=$(date +%s%N)

printf "\n${BLUE}Summary for %s tests:\n" "$total"
printf "\t${PASS} %s\n" "$passed"
printf "\t${FAIL} %s\n" "$failed"
printf "\n${LIGHT_BLUE}%s${BLUE} tests completed in ${LIGHT_BLUE}%sms\n" "$total" "$(((end-start)/1000000))"
printf "\n"

[ "$passed" = "$total" ] || exit 1