Convert tests/modules-test.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-05-12 16:46:45 +02:00 committed by Philip Withnall
parent a20a432100
commit f62be5660d

View File

@ -19,14 +19,10 @@
* Modified by the GLib Team and others 1997-2000. See the AUTHORS * Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog * file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with * files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/. * GLib at ftp://ftp.gtk.org/pub/gtk/.
*/ */
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <gmodule.h> #include <gmodule.h>
#include <string.h>
#ifdef _MSC_VER #ifdef _MSC_VER
# define MODULE_FILENAME_PREFIX "" # define MODULE_FILENAME_PREFIX ""
@ -55,30 +51,28 @@ compare (const gchar *desc, const gchar *expected, const gchar *found)
{ {
if (!expected && !found) if (!expected && !found)
return; return;
if (expected && found && strcmp (expected, found) == 0) if (expected && found && strcmp (expected, found) == 0)
return; return;
g_error ("error: %s state should have been \"%s\", but is \"%s\"", g_error ("error: %s state should have been \"%s\", but is \"%s\"",
desc, expected ? expected : "NULL", found ? found : "NULL"); desc, expected ? expected : "NULL", found ? found : "NULL");
} }
static void static void
test_states (const gchar *global, const gchar *gplugin_a, test_states (const gchar *global, const gchar *gplugin_a, const gchar *gplugin_b)
const gchar *gplugin_b) {
{
compare ("global", global, global_state); compare ("global", global, global_state);
compare ("Plugin A", gplugin_a, *gplugin_a_state); compare ("Plugin A", gplugin_a, *gplugin_a_state);
compare ("Plugin B", gplugin_b, *gplugin_b_state); compare ("Plugin B", gplugin_b, *gplugin_b_state);
global_state = *gplugin_a_state = *gplugin_b_state = NULL; global_state = *gplugin_a_state = *gplugin_b_state = NULL;
} }
static SimpleFunc plugin_clash_func = NULL; static SimpleFunc plugin_clash_func = NULL;
int static void
main (int argc, test_module_basics (void)
char **argv)
{ {
GModule *module_self, *module_a, *module_b; GModule *module_self, *module_a, *module_b;
gchar *plugin_a, *plugin_b; gchar *plugin_a, *plugin_b;
@ -86,8 +80,6 @@ main (int argc,
GModuleFunc gmod_f; GModuleFunc gmod_f;
GError *error = NULL; GError *error = NULL;
g_test_init (&argc, &argv, NULL);
if (!g_module_supported ()) if (!g_module_supported ())
g_error ("dynamic modules not supported"); g_error ("dynamic modules not supported");
@ -95,7 +87,7 @@ main (int argc,
plugin_b = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_b_" MODULE_TYPE, NULL); plugin_b = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_b_" MODULE_TYPE, NULL);
/* module handles */ /* module handles */
module_self = g_module_open_full (NULL, G_MODULE_BIND_LAZY, &error); module_self = g_module_open_full (NULL, G_MODULE_BIND_LAZY, &error);
g_assert_no_error (error); g_assert_no_error (error);
if (!module_self) if (!module_self)
@ -120,34 +112,33 @@ main (int argc,
/* get plugin state vars */ /* get plugin state vars */
if (!g_module_symbol (module_a, "gplugin_a_state", if (!g_module_symbol (module_a, "gplugin_a_state",
(gpointer *) &gplugin_a_state)) (gpointer *) &gplugin_a_state))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
if (!g_module_symbol (module_b, "gplugin_b_state", if (!g_module_symbol (module_b, "gplugin_b_state",
(gpointer *) &gplugin_b_state)) (gpointer *) &gplugin_b_state))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, "check-init"); test_states (NULL, NULL, "check-init");
/* get plugin specific symbols and call them /* get plugin specific symbols and call them */
*/
if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a)) if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, NULL); test_states (NULL, NULL, NULL);
if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b)) if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, NULL); test_states (NULL, NULL, NULL);
f_a (); f_a ();
test_states (NULL, "Hello world", NULL); test_states (NULL, "Hello world", NULL);
f_b (); f_b ();
test_states (NULL, NULL, "Hello world"); test_states (NULL, NULL, "Hello world");
/* get and call globally clashing functions /* get and call globally clashing functions */
*/
if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self)) if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, NULL); test_states (NULL, NULL, NULL);
@ -155,14 +146,14 @@ main (int argc,
if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a)) if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, NULL); test_states (NULL, NULL, NULL);
if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b)) if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
g_error ("error: %s", g_module_error ()); g_error ("error: %s", g_module_error ());
test_states (NULL, NULL, NULL); test_states (NULL, NULL, NULL);
f_self (); f_self ();
test_states ("global clash", NULL, NULL); test_states ("global clash", NULL, NULL);
f_a (); f_a ();
test_states (NULL, "global clash", NULL); test_states (NULL, "global clash", NULL);
@ -195,7 +186,7 @@ main (int argc,
gmod_f (module_b); gmod_f (module_b);
test_states (NULL, NULL, "BOOH"); test_states (NULL, NULL, "BOOH");
gmod_f (module_a); gmod_f (module_a);
test_states (NULL, "BOOH", NULL); test_states (NULL, "BOOH", NULL);
@ -210,5 +201,14 @@ main (int argc,
g_free (plugin_a); g_free (plugin_a);
g_free (plugin_b); g_free (plugin_b);
g_module_close (module_self); g_module_close (module_self);
return 0; }
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/module/basics", test_module_basics);
return g_test_run ();
} }