aboutsummaryrefslogtreecommitdiffstats
path: root/mtest.sh
blob: 105df9375949af0e78d33040720b53f69007b7b8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh

## ==========================================================================
#   Licensed under BSD 2clause license See LICENSE file for more information
#   Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
#  ==========================================================================
#    __________________________________________________________
#   /                   mtest version v1.1.3                   \
#   |                                                          |
#   |    Simple test framework that uses TAP output format     |
#   \                 http://testanything.org                  /
#    ----------------------------------------------------------
#          \
#           \
#            \        .
#             .---.  //
#            Y|o o|Y//
#           /_(i=i)K/
#           ~()~*~()~
#            (_)-(_)
#
#        Darth Vader Koala
## ==========================================================================


## ==========================================================================
#                                   __               __
#                       ____ ___   / /_ ___   _____ / /_
#                      / __ `__ \ / __// _ \ / ___// __/
#                     / / / / / // /_ /  __/(__  )/ /_
#                    /_/ /_/ /_/ \__/ \___//____/ \__/
#
#                                  _         __     __
#             _   __ ____ _ _____ (_)____ _ / /_   / /___   _____
#            | | / // __ `// ___// // __ `// __ \ / // _ \ / ___/
#            | |/ // /_/ // /   / // /_/ // /_/ // //  __/(__  )
#            |___/ \__,_//_/   /_/ \__,_//_.___//_/ \___//____/
#
## ==========================================================================


mt_test_status=0
mt_total_tests=0
mt_total_failed=0
mt_total_checks=0
mt_checks_failed=0
mt_current_test="none"


## ==========================================================================
#                                       __     __ _
#                        ____   __  __ / /_   / /(_)_____
#                       / __ \ / / / // __ \ / // // ___/
#                      / /_/ // /_/ // /_/ // // // /__
#                     / .___/ \__,_//_.___//_//_/ \___/
#                    /_/
#              ____                     __   _
#             / __/__  __ ____   _____ / /_ (_)____   ____   _____
#            / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/
#           / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__  )
#          /_/   \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/
#
## ==========================================================================


## ==========================================================================
#   run specified test
#
#   $1 - function name as a string - will be passed to eval
## ==========================================================================


mt_run()
{
    mt_run_named $1 $1
}


## ==========================================================================
#   run specified test with custom name to be printed during report
#
#   $1 - function name as a string - will be passed to eval
#   $2 - test name, will be used instead of $1 in report
## ==========================================================================


mt_run_named()
{
    mt_current_test="$2"
    mt_test_status=0
    mt_total_tests=$((mt_total_tests + 1))

    if type mt_prepare_test > /dev/null 2>&1
    then
        mt_prepare_test
    fi

    eval "$1"

    if type mt_cleanup_test > /dev/null 2>&1
    then
        mt_cleanup_test
    fi

    if [ $mt_test_status -ne 0 ]
    then
        echo "not ok $mt_total_tests - $mt_current_test"
        mt_total_failed=$((mt_total_failed + 1))
    else
        echo "ok $mt_total_tests - $mt_current_test"
    fi
}


## ==========================================================================
#   performs check on given command, if command returns error,  current test
#   will be marked as failed.
#
#   $1 - code to evaluate, simply passed to eval
## ==========================================================================


mt_fail()
{
    ((mt_total_checks++))
    if ! eval $1
    then
        echo "# assert $mt_current_test, '$1'"
        mt_test_status=1
        ((mt_checks_failed++))
    fi
}


## ==========================================================================
#   prints test plant in  format 1..<number_of_test_run>.  If all tests have
#   passed,  macro will exit script with code 0,  else  it returns number of
#   failed tests.  If number of failed tests  exceeds 254,  then 254 will be
#   returned.
#
#   This function should be called when all tests have been run
## ==========================================================================


mt_return()
{
    echo "1..$mt_total_tests"

    mt_passed_tests=$((mt_total_tests - mt_total_failed))
    mt_passed_checks=$((mt_total_checks - mt_checks_failed))

    printf "# total tests.......: %4d\n" ${mt_total_tests}
    printf "# passed tests......: %4d\n" ${mt_passed_tests}
    printf "# failed tests......: %4d\n" ${mt_total_failed}
    printf "# total checks......: %4d\n" ${mt_total_checks}
    printf "# passed checks.....: %4d\n" ${mt_passed_checks}
    printf "# failed checks.....: %4d\n" ${mt_checks_failed}

    if [ $mt_total_failed -gt 254 ]
    then
        exit 254
    else
        exit $mt_total_failed
    fi
}