Debian Codegraph @

subprojects/mutest/docs/mutest-matchers.md.html

raw symbols SCIP
1 <meta charset="utf-8"><link rel="stylesheet" href="apidoc.css"/>
2
3 **µTest — A small C testing library**
4
5 * [Main](./mutest.md.html)
6
7 ## Matchers
8
9 _Matchers_ are functions passed to [`mutest_expect()`](mutest-general.md.html#//functions/mutest_expect).
10 A matcher function takes a pointer to the expectation object, and a pointer to
11 a value wrapper which containes the expected value.
12
13 If the value contained in the expectation object matches the expected value,
14 the the matcher is satisfied. If all matcher functions passed to
15 [`mutest_expect()`](mutest-general.md.html#//functions/mutest_expect) are
16 satisfied, the expectation is satisfied.
17
18 For instance, the following expectation:
19
20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 const char *str = "hello, world";
22
23 mutest_expect ("the string to match a greeting",
24 mutest_string_value (str),
25 mutest_to_be, "hello, world",
26 NULL);
27 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28
29 Contains a single matcher, [`mutest_to_be()`](#/matchers/functions/mutest_to_be). The
30 [`mutest_to_be()`](#//functions/mutest_to_be) matcher is satisfied if the value
31 in `str` and wrapped by [`mutest_string_value()`](mutest-wrappers.md.html#/valuewrappers/functions/mutest_string_value)
32 matches the function parameter, following [`mutest_to_be()`](#/matchers/functions/mutest_to_be)
33 in the expectation.
34
35 In the following example:
36
37 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38 const char *str = "hello, world";
39
40 mutest_expect ("the string to contain all parts of the greeting",
41 mutest_string_value (str),
42 mutest_to_start_with_string, "hello",
43 mutest_to_contain, ",",
44 mutest_to_end_with_string, "world",
45 NULL);
46 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47
48 The expectation is composed of three separate matchers:
49
50 - [`mutest_to_start_with_string()`](#/matchers/functions/mutest_to_start_with_string),
51 which checks that the string value starts with the matcher parameter
52 - [`mutest_to_contain()`](#/matchers/functions/mutest_to_contain), which checks that
53 the string value contains the matcher parameter
54 - [`mutest_to_end_with_string()`](#/matchers/functions/mutest_to_end_with_string),
55 which checks that the string value ends with the matcher parameter
56
57 The expectation, in the case above, is only satisfied if all three
58 matcher functions are satisfied.
59
60 !!! Note
61 Matcher functions provided by µTest can automatically collect the
62 expected values from the variadic arguments list passed to
63 [`mutest_expect()`](mutest-general.md.html#//functions/mutest_expect)
64 without explicitly wrapping them with a `mutest_expect_res_t`. Custom
65 matchers, on the other hand, require an explicit value wrapper.
66
67 ### Includes
68
69 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 #include <mutest.h>
71 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
73 ### Functions
74
75 #### `mutest_not`
76
77 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78 bool
79 mutest_not (mutest_expect_t *e,
80 mutest_expect_res_t *check);
81 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82
83 A special matcher function that negates the condition of the following matcher
84 function.
85
86 For instance:
87
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 mutest_expect ("a to not be true",
90 mutest_bool_value (false),
91 mutest_not, mutest_to_be, true,
92 NULL);
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95 e
96 : the expectation object
97 check
98 : the matcher argument
99 return value
100 : `true` if the matcher is satisfied, and `false` otherwise
101
102 ----
103
104 #### `mutest_to_be`
105
106 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 bool
108 mutest_to_be (mutest_expect_t *e,
109 mutest_expect_res_t *check);
110 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
112 Matches the value in `e` to the value in `check` exactly.
113
114 This matcher collects a value that matches the type of the value
115 passed to `mutest_expect()`.
116
117 e
118 : the expectation object
119 check
120 : the matcher argument
121 return value
122 : `true` if the matcher is satisfied, and `false` otherwise
123
124 ----
125
126 #### `mutest_to_be_close_to`
127
128 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129 bool
130 mutest_to_be_close_to (mutest_expect_t *e,
131 mutest_expect_res_t *check);
132 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133
134 Checks whether the value in `e` is within a given tolerance of the value
135 wrapped by `check`.
136
137 This matcher collects two values:
138
139 - the numeric value to be used in the comparison
140 - the numeric value to be used as a tolerance
141
142 e.g.
143
144 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145 mutest_expect ("angle to be close to π",
146 mutest_float_value (angle),
147 mutest_to_be_close_to, M_PI, 0.0001,
148 NULL);
149 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150
151 e
152 : the expectation object
153 check
154 : the matcher argument
155 return value
156 : `true` if the matcher is satisfied, and `false` otherwise
157
158 ----
159
160 #### `mutest_to_be_nan`
161
162 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163 bool
164 mutest_to_be_nan (mutest_expect_t *e,
165 mutest_expect_res_t *check);
166 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167
168 Checks whether the floating point value in `e` is NaN (Not a Number).
169
170 This matcher collects the numeric value to be used in the comparison.
171
172 e
173 : the expectation object
174 check
175 : the matcher argument
176 return value
177 : `true` if the matcher is satisfied, and `false` otherwise
178
179 ----
180
181 #### `mutest_to_be_positive_infinity`
182
183 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 bool
185 mutest_to_be_positive_infinity (mutest_expect_t *e,
186 mutest_expect_res_t *check);
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188
189 Checks whether the floating point value in `e` is a positive infinity.
190
191 This matcher collects the numeric value to be used in the comparison.
192
193 e
194 : the expectation object
195 check
196 : the matcher argument
197 return value
198 : `true` if the matcher is satisfied, and `false` otherwise
199
200 ----
201
202 #### `mutest_to_be_negative_infinity`
203
204 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205 bool
206 mutest_to_be_negative_infinity (mutest_expect_t *e,
207 mutest_expect_res_t *check);
208 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209
210 Checks whether the floating point value in `e` is a negative infinity.
211
212 This matcher collects the numeric value to be used in the comparison.
213
214 e
215 : the expectation object
216 check
217 : the matcher argument
218 return value
219 : `true` if the matcher is satisfied, and `false` otherwise
220
221 ----
222
223 #### `mutest_to_be_greater_than`
224
225 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226 bool
227 mutest_to_be_greater_than (mutest_expect_t *e,
228 mutest_expect_res_t *check);
229 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230
231 Checks whether the numeric value in `e` is greater than the value in `check`.
232
233 This matcher collects the numeric value to be used in the comparison.
234
235 e
236 : the expectation object
237 check
238 : the matcher argument
239 return value
240 : `true` if the matcher is satisfied, and `false` otherwise
241
242 ----
243
244 #### `mutest_to_be_greater_than_or_equal`
245
246 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247 bool
248 mutest_to_be_greater_than_or_equal (mutest_expect_t *e,
249 mutest_expect_res_t *check);
250 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
251
252 Checks whether the numeric value in `e` is greater than, or equal to
253 the value in `check`.
254
255 This matcher collects the numeric value to be used in the comparison.
256
257 e
258 : the expectation object
259 check
260 : the matcher argument
261 return value
262 : `true` if the matcher is satisfied, and `false` otherwise
263
264 ----
265
266 #### `mutest_to_be_less_than_or_equal`
267
268 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269 bool
270 mutest_to_be_less_than_or_equal (mutest_expect_t *e,
271 mutest_expect_res_t *check);
272 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273
274 Checks whether the numeric value in `e` is less than, or equal to
275 the value in `check`.
276
277 This matcher collects the numeric value to be used in the comparison.
278
279 e
280 : the expectation object
281 check
282 : the matcher argument
283 return value
284 : `true` if the matcher is satisfied, and `false` otherwise
285
286 ----
287
288 #### `mutest_to_be_less_than`
289
290 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291 bool
292 mutest_to_be_less_than (mutest_expect_t *e,
293 mutest_expect_res_t *check);
294 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
295
296 Checks whether the numeric value in `e` is less than the value in `check`.
297
298 This matcher collects the numeric value to be used in the comparison.
299
300 e
301 : the expectation object
302 check
303 : the matcher argument
304 return value
305 : `true` if the matcher is satisfied, and `false` otherwise
306
307 ----
308
309 #### `mutest_to_be_in_range`
310
311 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312 bool
313 mutest_to_be_in_range (mutest_expect_t *e,
314 mutest_expect_res_t *check);
315 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
316
317 Checks if the numeric value in `e` is inside the range in `check`.
318
319 This matcher collects the numeric range as two values, e.g.
320
321 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
322 mutest_expect ("number to be between 0 and 100",
323 mutest_int_value (random_number),
324 mutest_to_be_in_range, 0, 100,
325 NULL);
326 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327
328 e
329 : the expectation object
330 check
331 : the matcher argument
332 return value
333 : `true` if the matcher is satisfied, and `false` otherwise
334
335 ----
336
337 #### `mutest_to_contain`
338
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340 bool
341 mutest_to_contain (mutest_expect_t *e,
342 mutest_expect_res_t *check);
343 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344
345 Checks if the numeric range, or string, in `e` contains the numeric
346 value, or string, in `check`.
347
348 This matcher collects the numeric or string value that should be
349 contained in the numeric range or string value passed to `mutest_expect()`.
350
351 e
352 : the expectation object
353 check
354 : the matcher argument
355 return value
356 : `true` if the matcher is satisfied, and `false` otherwise
357
358 ----
359
360 #### `mutest_to_start_with_string`
361
362 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
363 bool
364 mutest_to_start_with_string (mutest_expect_t *e,
365 mutest_expect_res_t *check);
366 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367
368 Checks that the string in `e` starts with the string in `check`.
369
370 This matcher collects the expected string that should be at
371 the beginning of the string value passed to `mutest_expect()`.
372
373 e
374 : the expectation object
375 check
376 : the matcher argument
377 return value
378 : `true` if the matcher is satisfied, and `false` otherwise
379
380 ----
381
382 #### `mutest_to_end_with_string`
383
384 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
385 bool
386 mutest_to_end_with_string (mutest_expect_t *e,
387 mutest_expect_res_t *check);
388 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389
390 Checks that the string in `e` ends with the string in `check`.
391
392 This matcher collects the expected string that should be at
393 the end of the string value passed to `mutest_expect()`.
394
395 e
396 : the expectation object
397 check
398 : the matcher argument
399 return value
400 : `true` if the matcher is satisfied, and `false` otherwise
401
402 ----
403
404 #### `mutest_to_be_true`
405
406 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407 bool
408 mutest_to_be_true (mutest_expect_t *e,
409 mutest_expect_res_t *check);
410 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
411
412 Checks that the boolean value in `e` is true.
413
414 This matcher does not collect any value.
415
416 e
417 : the expectation object
418 check
419 : the matcher argument
420 return value
421 : `true` if the matcher is satisfied, and `false` otherwise
422
423 ----
424
425 #### `mutest_to_be_false`
426
427 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
428 bool
429 mutest_to_be_false (mutest_expect_t *e,
430 mutest_expect_res_t *check);
431 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
432
433 Checks that the boolean value in `e` is false.
434
435 This matcher does not collect any value.
436
437 e
438 : the expectation object
439 check
440 : the matcher argument
441 return value
442 : `true` if the matcher is satisfied, and `false` otherwise
443
444 ----
445
446 #### `mutest_to_be_null`
447
448 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
449 bool
450 mutest_to_be_null (mutest_expect_t *e,
451 mutest_expect_res_t *check);
452 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
453
454 Checks that the pointer value in `e` is `NULL`.
455
456 This matcher does not collect any value.
457
458 e
459 : the expectation object
460 check
461 : the matcher argument
462 return value
463 : `true` if the matcher is satisfied, and `false` otherwise
464
465 ----
466
467 #### `mutest_skip`
468
469 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
470 bool
471 mutest_skip (mutest_expect_t *e,
472 mutest_expect_res_t *check);
473 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
474
475 A special matcher function that will cause the expectation to be marked as
476 skipped.
477
478 This matcher collects a string that contains a human readable reason for
479 the expectation to be skipped.
480
481 You should use this matcher as the only one in an expectation.
482
483 e
484 : the expectation object
485 check
486 : the matcher argument
487 return value
488 : `true` if the matcher is satisfied, and `false` otherwise
489
490 ----
491
492 #### `mutest_expect_value`
493
494 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
495 mutest_expect_res_t *
496 mutest_expect_value (mutest_expect_t *expect);
497 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
498
499 Retrieves the pointer to the value wrapper passed to `mutest_expect()` from
500 the `mutest_expect_t` object.
501
502 This function is typically used when implementing custom matchers. For
503 instance:
504
505 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
506 static bool
507 test_my_object_is_equal (mutest_expect_t *e,
508 mutest_expect_res_t *check)
509 {
510 mutest_expect_res_t *value = mutest_expect_value (e);
511
512 MyObject *a = mutest_get_pointer (value);
513 MyObject *b = mutest_get_pointer (check);
514
515 // Simple equality
516 if (a == b)
517 return true;
518
519 // my_object_deep_equal() is defined elsewhere as a deep
520 // equality of two MyObject instances
521 return my_object_deep_equal (a, b);
522 }
523
524 ...
525
526 // Custom matchers must use the explicit value wrappers,
527 // as µTest doesn't know how to collect the values
528 mutest_expect ("the objects to be equal",
529 mutest_pointer (foo),
530 test_my_object_is_equal, mutest_pointer (bar),
531 NULL);
532 ...
533 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
534
535 ### Types
536
537 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
538 typedef bool
539 (* mutest_matcher_func_t) (mutest_expect_t *e,
540 mutest_expect_res_t *check);
541 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
542
543 e
544 : the expectation object
545 check
546 : the matcher argument
547 return value
548 : `true` if the matcher is satisfied, and `false` otherwise
549
550 The prototype of a matcher function to pass to `mutest_expect()`.
551
552 ----
553
554 <style class="fallback">body{visibility:hidden}</style><script>markdeepOptions={tocStyle:'medium'};</script>
555 <!-- Markdeep: --><script src="markdeep.min.js" charset="utf-8"></script>