aboutsummaryrefslogtreecommitdiffstats
path: root/man/mt_run.3
blob: b7a2eee786ee0acdc14ce96c17394512c31aa1e8 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
.TH "MT_RUN" "3" "13 February 2021 (v1.4.0)" "bofc.pl"
.SH NAME
.PP
.B mt_run
- runs specific test
.SH SYNOPSIS
.PP
c/c++
.PP
.B #include <mtest.h>
.PP
.BI "void (*" function_name ")(void)"
.br
.BI "void (*" function_name_param ")(void *" parameter ")"
.PP
.BI "mt_run(" function_name ")"
.br
.BI "mt_run_param(" function_name_param ", " parameter ")"
.br
.BI "mt_run_named(" function_name ", " test_name ")"
.br
.BI "mt_run_param_named(" function_name_param ", " parameter ", " test_name ")"
.br
.BI "mt_run_quick(" expression ")"
.br
.BI "mt_run_quick_named(" expression ", " test_name ")"
.PP
.BI "static void (*" mt_prepare_test ")(void)"
.br
.BI "static void (*" mt_cleanup_test ")(void)"
.PP
shell
.PP
.BI "mt_run <" function_name "> [" param1 "] [" ... "]"
.br
.BI "mt_run_named <" function_name "> <" test_name "> [" param1 "] \
[" ... "]"
.SH DESCRIPTION
.PP
.BR mt_run (3)
runs a single test specified in
.IR function_name .
Inside function you can call
.BR mt_assert (3)
and
.BR mt_fail (3).
If none if these functions are called inside test function,
.B mtest
will mark test as successful.
After function finishes its work,
.BR mt_run (3)
will print test status and a
.I function_name
to know which test passed or failed.
.PP
.BR mt_run_named (3)
works similar, but also takes
.IR test_name ,
that will be printed instead of
.I function_name
when reporting test results.
.I test_name
should be simple
.B const char *.
.PP
.BR mt_run_param (3)
and
.BR mt_run_param_named (3)
works in the same way as they non-parameter counterpart, but
.I parameter
will be passed to
.IR function_name_param .
When using shell functions, there is no
.I param
functions, but you can pass unspecified number of parameters to both
.BR mt_run (3)
and
.BR mt_run_named (3).
Since
.I function_name
is run via
.B eval
program, when you want to pass parameter with spaces, you need to use
double quotes like
.BR """'parameter\ with\ spaces'""" .
.PP
.BR mt_run_quick (3)
allows to perform quick test with only single
.I expression
check.
It initializes tests with
.IR mt_prepare_test and mt_cleanup_test
but does only single
.BR mt_fail (3)
on
.IR expresssion .
This is effectively same as doing
.PP
.EX
    void foo(void) { mt_fail(e); }
    void test_group(void) { mt_run(foo); }
.EE
.PP
Now, of course you could simply do multiple
.BR mt_fail (3)
calls but with that you loose some context, especially with
.BR mt_run_quick_named (3)
variant of the function.
.PP
.BR mt_run_quick_named (3)
works in the same way as
.BR mt_run_quick (3)
but also allows to specify custom test name for better test analysis.
Like said before, it is better to use this instead of multiple
.BR mt_fail (3)
checks to get some more context.
.PP
.EX
    /* It is easier to see what broke if you use it like */
    void test_group(void) {
        mt_run(foo_init(arg1) == 0,  "foo_init should succeed");
        mt_run(foo_init(NULL) == -1, "foo_init should fail when arg1 = NULL");
    }

    /* instead of doing */
    void foo_init_tests(void) {
        mt_fail(foo_init(arg1) == 0);
        mt_fail(foo_init(NULL) == -1);
    }
.EE
.PP
Optionally user can also set two function pointers
.I mt_prepare_test
and
.I mt_cleanup_test
that take no argument and return nothing.
These functions will be called before and after calling test
.IR function_name .
.PP
When testing from shell, it is only neccessary to define functions
.I mt_prepare_test
and
.I mt_cleanup_test
and
.B mtest
will use then automatically
.SH EXAMPLE
.PP
c/c++
.PP
.EX
    #include <mtest.h>
    #include "foo.h"

    mt_defs();

    static void test_param(void *param)
    {
        mt_assert(bar((int *)i) == 0);
    }

    static void test(void)
    {
        mt_assert(foo() == 0);
    }

    int main(void)
    {
        int i;

        mt_run(test);
        mt_run_named(test, "test param 1");
        mt_run_named(test, "test_param 2");

        for (i = 0; i != 5; ++i)
        {
            mt_run_param(test_param, &i);
        }

        mt_return();
    }
.EX
.PP
shell
.PP
.EX
    #!/bin/sh

    . ./mtest.sh

    test_two()
    {
        a=$1
        b=$2
        c=$((a + b))
        mt_fail "[ $b -eq $(( c - a )) ]"
    }

    test_one()
    {
        a=1
        a=$((a + 1))
        mt_fail "[ $a -eq 2 ]"
    }

    mt_run test_one
    mt_run test_two 5 3

    mt_return
.EE
.SH "SEE ALSO"
.PP
.BR mt_defs (3),
.BR mt_defs_ext (3),
.BR mt_assert (3),
.BR mt_fail (3),
.BR mt_fok (3),
.BR mt_ferr (3),
.BR mt_return (3)
.BR mtest_overview (7),