gtestutils: Add a new g_assert_no_errno() test macro

This is for use in testing POSIX-style functions like `rmdir()`, which
return an integer < 0 on failure, and return their error information in
`errno`.

The new macro prints `errno` and `g_strerror (errno)` on failure.

Includes a unit test.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall
2019-10-31 11:22:16 +00:00
parent 2510d5aae0
commit 9e45b95816
4 changed files with 61 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
#include <glib/gstring.h>
#include <glib/gerror.h>
#include <glib/gslist.h>
#include <errno.h>
#include <string.h>
G_BEGIN_DECLS
@@ -110,6 +111,20 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
} \
} \
G_STMT_END
#define g_assert_no_errno(expr) G_STMT_START { \
int __ret, __errsv; \
errno = 0; \
__ret = expr; \
__errsv = errno; \
if (__ret < 0) \
{ \
gchar *__msg; \
__msg = g_strdup_printf ("assertion failed (" #expr " >= 0): errno %i: %s", __errsv, g_strerror (__errsv)); \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
g_free (__msg); \
} \
} G_STMT_END \
GLIB_AVAILABLE_MACRO_IN_2_66
#define g_assert_no_error(err) G_STMT_START { \
if (err) \
g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \