Skip to content
Snippets Groups Projects
Commit 62ae8bf3 authored by Vladimir Davydov's avatar Vladimir Davydov Committed by Vladimir Davydov
Browse files

test: allow to omit message in ok/is/isnt unit test helpers

It's really annoying to add a message after each check in a unit test.
Let's make this optional. If the message is omitted, "line %d" will be
used instead. Also, let's print the expression on failure because it
may be useful if exact sources are unavailable.

NO_DOC=refactoring
NO_CHANGELOG=refactoring
parent 4871b697
No related branches found
No related tags found
No related merge requests found
...@@ -60,19 +60,24 @@ check_plan(void) ...@@ -60,19 +60,24 @@ check_plan(void)
return r; return r;
} }
int void
_ok(int condition, const char *fmt, ...) _ok(int condition, const char *expr, const char *file, int line,
const char *fmt, ...)
{ {
va_list ap; va_list ap;
_space(stdout); _space(stdout);
printf("%s %d - ", condition ? "ok" : "not ok", ++tests_done[level]); printf("%s %d - ", condition ? "ok" : "not ok", ++tests_done[level]);
if (!condition)
tests_failed[level]++;
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
printf("\n"); printf("\n");
va_end(ap); va_end(ap);
return condition; if (!condition) {
tests_failed[level]++;
_space(stderr);
fprintf(stderr, "# Failed test `%s'\n", expr);
_space(stderr);
fprintf(stderr, "# in %s at line %d\n", file, line);
}
} }
...@@ -70,7 +70,9 @@ extern "C" { ...@@ -70,7 +70,9 @@ extern "C" {
*/ */
/* private function, use ok(...) instead */ /* private function, use ok(...) instead */
int _ok(int condition, const char *fmt, ...); void
_ok(int condition, const char *expr, const char *file, int line,
const char *fmt, ...);
/* private function, use note(...) or diag(...) instead */ /* private function, use note(...) or diag(...) instead */
void _space(FILE *stream); void _space(FILE *stream);
...@@ -90,41 +92,31 @@ _plan(int count, bool tap); ...@@ -90,41 +92,31 @@ _plan(int count, bool tap);
*/ */
int check_plan(void); int check_plan(void);
#define ok(condition, fmt, args...) { \ /*
int res = _ok(condition, fmt, ##args); \ * The ok macro is defined so that it can be called without a message:
if (!res) { \ *
_space(stderr); \ * ok(true);
fprintf(stderr, "# Failed test '"); \ * ok(true, "message");
fprintf(stderr, fmt, ##args); \ * ok(true, "message %d", i);
fprintf(stderr, "'\n"); \ *
_space(stderr); \ * It supports up to 7 format arguments.
fprintf(stderr, "# in %s at line %d\n", __FILE__, __LINE__); \ */
} \ #define _select_10th(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, ...) f10
} #define _ok0(cond, expr, ...) \
_select_10th(, ##__VA_ARGS__, \
#define is(a, b, fmt, args...) { \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
int res = _ok((a) == (b), fmt, ##args); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
if (!res) { \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
_space(stderr); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
fprintf(stderr, "# Failed test '"); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
fprintf(stderr, fmt, ##args); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
fprintf(stderr, "'\n"); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
_space(stderr); \ _ok(cond, expr, __FILE__, __LINE__, __VA_ARGS__), \
fprintf(stderr, "# in %s at line %d\n", __FILE__, __LINE__); \ _ok(cond, expr, __FILE__, __LINE__, "line %d", __LINE__))
} \
} #define ok(cond, ...) _ok0(cond, #cond, ##__VA_ARGS__)
#define is(a, b, ...) _ok0((a) == (b), #a " == " #b, ##__VA_ARGS__)
#define isnt(a, b, fmt, args...) { \ #define isnt(a, b, ...) _ok0((a) != (b), #a " != " #b, ##__VA_ARGS__)
int res = _ok((a) != (b), fmt, ##args); \
if (!res) { \
_space(stderr); \
fprintf(stderr, "# Failed test '"); \
fprintf(stderr, fmt, ##args); \
fprintf(stderr, "'\n"); \
_space(stderr); \
fprintf(stderr, "# in %s at line %d\n", __FILE__, __LINE__); \
} \
}
#if UNIT_TAP_COMPATIBLE #if UNIT_TAP_COMPATIBLE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment