diff options
author | Philip Withnall <withnall@endlessm.com> | 2018-07-11 17:30:24 +0200 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2018-07-11 17:41:46 +0200 |
commit | ca23acdb24b568ed98257f1c462eb83ca527a205 (patch) | |
tree | d5fc4bf19c4a1de2b124dfad1a2324c38345e52a /glib/gtestutils.h | |
parent | 51ce8d204c62bdc67f7d6f5816b3192f43ac6175 (diff) | |
download | glib-ca23acdb24b568ed98257f1c462eb83ca527a205.tar.gz |
gtestutils: Bail out of g_test_init() if G_DISABLE_ASSERT is defined
If G_DISABLE_ASSERT is defined, g_assert() is a no-op. Despite it now
being standard practice to *not* use g_assert() in unit tests (use
g_assert_*() instead), a lot of existing unit tests still use it.
Compiling those tests with G_DISABLE_ASSERT would make them silently
no-ops. Avoid that by warning the user loudly.
Note that it’s pretty rare for people to compile with G_DISABLE_ASSERT,
so it’s not expected that this will be hit often.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://gitlab.gnome.org/GNOME/glib/issues/976
Diffstat (limited to 'glib/gtestutils.h')
-rw-r--r-- | glib/gtestutils.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/glib/gtestutils.h b/glib/gtestutils.h index b247f2f3a..550e2414e 100644 --- a/glib/gtestutils.h +++ b/glib/gtestutils.h @@ -147,6 +147,29 @@ GLIB_AVAILABLE_IN_ALL void g_test_init (int *argc, char ***argv, ...) G_GNUC_NULL_TERMINATED; + +/* While we discourage its use, g_assert() is often used in unit tests + * (especially in legacy code). g_assert_*() should really be used instead. + * g_assert() can be disabled at client program compile time, which can render + * tests useless. Highlight that to the user. */ +#ifdef G_DISABLE_ASSERT +#if defined(G_HAVE_ISO_VARARGS) +#define g_test_init(argc, argv, ...) \ + G_STMT_START { \ + g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \ + exit (1); \ + } G_STMT_END +#elif defined(G_HAVE_GNUC_VARARGS) +#define g_test_init(argc, argv...) \ + G_STMT_START { \ + g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \ + exit (1); \ + } G_STMT_END +#else /* no varargs */ + /* do nothing */ +#endif /* varargs support */ +#endif /* G_DISABLE_ASSERT */ + /* query testing framework config */ #define g_test_initialized() (g_test_config_vars->test_initialized) #define g_test_quick() (g_test_config_vars->test_quick) |