From 8aeb391a771302fa6818a37a76e1240c955ce8f5 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 24 Dec 2013 00:03:20 -0500 Subject: [PATCH] Improve GIOModule test coverage Add a dedicated test for GIOModule. --- configure.ac | 1 + gio/tests/Makefile.am | 5 +- gio/tests/giomodule.c | 136 ++++++++++++++++++++++++++++++ gio/tests/modules/Makefile.am | 27 ++++++ gio/tests/modules/test-module-a.c | 57 +++++++++++++ gio/tests/modules/test-module-b.c | 57 +++++++++++++ 6 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 gio/tests/giomodule.c create mode 100644 gio/tests/modules/Makefile.am create mode 100644 gio/tests/modules/test-module-a.c create mode 100644 gio/tests/modules/test-module-b.c diff --git a/configure.ac b/configure.ac index 603e377ec..e519cd12d 100644 --- a/configure.ac +++ b/configure.ac @@ -3604,6 +3604,7 @@ gio/tests/Makefile gio/tests/gdbus-object-manager-example/Makefile gio/tests/services/Makefile gio/tests/services/org.gtk.GDBus.Examples.ObjectManager.service +gio/tests/modules/Makefile po/Makefile.in docs/Makefile docs/reference/Makefile diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am index bffbe8dee..220e6f8dd 100644 --- a/gio/tests/Makefile.am +++ b/gio/tests/Makefile.am @@ -3,7 +3,7 @@ include $(top_srcdir)/glib-tap.mk dist_uninstalled_test_data = test_ltlibraries = -SUBDIRS = gdbus-object-manager-example services +SUBDIRS = gdbus-object-manager-example services modules LDADD = \ $(top_builddir)/gio/libgio-2.0.la \ @@ -34,6 +34,7 @@ test_programs = \ defaultvalue \ fileattributematcher \ filter-streams \ + giomodule \ gsubprocess \ g-file \ g-file-info \ @@ -536,7 +537,7 @@ giotypefuncs.c: Makefile echo "G_GNUC_BEGIN_IGNORE_DEPRECATIONS" > xgen-gio && \ ${CPP} $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) xgen-giosrc.c | \ $(GREP) -o '\bg_.*_get_type\b' | \ - $(GREP) -v 'g_io_extension_get_type\|g_variant_get_type' | \ + $(GREP) -v 'g_io_extension_get_type\|g_variant_get_type' | \ sort | uniq | \ $(SED) -e 's/^/*tp++ = /' -e 's/$$/ ();/' >> xgen-gio && \ cp xgen-gio $@ # && rm -f xgen-gio xgen-giosrc.c diff --git a/gio/tests/giomodule.c b/gio/tests/giomodule.c new file mode 100644 index 000000000..1b2eb6fe9 --- /dev/null +++ b/gio/tests/giomodule.c @@ -0,0 +1,136 @@ +/* Unit tests for GIOModule + * Copyright (C) 2013 Red Hat, Inc + * Author: Matthias Clasen + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + */ + +#include + +static void +test_extension_point (void) +{ + GIOExtensionPoint *ep, *ep2; + GIOExtension *ext; + GList *list; + GType req; + GTypeClass *class; + + ep = g_io_extension_point_lookup ("test-extension-point"); + g_assert_null (ep); + ep = g_io_extension_point_register ("test-extension-point"); + ep2 = g_io_extension_point_lookup ("test-extension-point"); + g_assert (ep2 == ep); + + req = g_io_extension_point_get_required_type (ep); + g_assert (req == G_TYPE_INVALID); + g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT); + req = g_io_extension_point_get_required_type (ep); + g_assert (req == G_TYPE_OBJECT); + + list = g_io_extension_point_get_extensions (ep); + g_assert_null (list); + + g_io_extension_point_implement ("test-extension-point", + G_TYPE_VFS, + "extension1", + 10); + + g_io_extension_point_implement ("test-extension-point", + G_TYPE_OBJECT, + "extension2", + 20); + + list = g_io_extension_point_get_extensions (ep); + g_assert_cmpint (g_list_length (list), ==, 2); + + ext = list->data; + g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2"); + g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT); + g_assert (g_io_extension_get_priority (ext) == 20); + class = g_io_extension_ref_class (ext); + g_assert (class == g_type_class_peek (G_TYPE_OBJECT)); + g_type_class_unref (class); + + ext = list->next->data; + g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1"); + g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS); + g_assert (g_io_extension_get_priority (ext) == 10); +} + +static void +test_module_scan_all (void) +{ + if (g_test_subprocess ()) + { + GIOExtensionPoint *ep; + GIOExtension *ext; + GList *list; + ep = g_io_extension_point_register ("test-extension-point"); + g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_DIST, "modules", NULL)); + g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_DIST, "modules/.libs", NULL)); + list = g_io_extension_point_get_extensions (ep); + g_assert_cmpint (g_list_length (list), ==, 2); + ext = list->data; + g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b"); + ext = list->next->data; + g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a"); + return; + } + g_test_trap_subprocess (NULL, 0, 7); + g_test_trap_assert_passed (); +} + +static void +test_module_scan_all_with_scope (void) +{ + if (g_test_subprocess ()) + { + GIOExtensionPoint *ep; + GIOModuleScope *scope; + GIOExtension *ext; + GList *list; + + ep = g_io_extension_point_register ("test-extension-point"); + scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES); + g_io_module_scope_block (scope, "libtestmoduleb.so"); + g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_DIST, "modules", NULL), scope); + list = g_io_extension_point_get_extensions (ep); + g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_DIST, "modules/.libs", NULL), scope); + list = g_io_extension_point_get_extensions (ep); + g_assert_cmpint (g_list_length (list), ==, 1); + ext = list->data; + g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a"); + g_io_module_scope_free (scope); + return; + } + g_test_trap_subprocess (NULL, 0, 7); + g_test_trap_assert_passed (); +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/giomodule/extension-point", test_extension_point); + g_test_add_func ("/giomodule/module-scan-all", test_module_scan_all); + g_test_add_func ("/giomodule/module-scan-all-with-scope", test_module_scan_all_with_scope); + + return g_test_run (); +} diff --git a/gio/tests/modules/Makefile.am b/gio/tests/modules/Makefile.am new file mode 100644 index 000000000..95e486595 --- /dev/null +++ b/gio/tests/modules/Makefile.am @@ -0,0 +1,27 @@ +NULL = + +LDADD = \ + $(top_builddir)/gio/libgio-2.0.la \ + $(top_builddir)/gobject/libgobject-2.0.la \ + $(top_builddir)/gmodule/libgmodule-2.0.la \ + $(top_builddir)/glib/libglib-2.0.la \ + $(NULL) + + +AM_CPPFLAGS = \ + $(gio_INCLUDES) $(GLIB_DEBUG_FLAGS) \ + -I$(top_builddir)/gio \ + -I$(top_srcdir)/gio + +testmoduledir = $(installed_testdir)/modules +testmodule_LTLIBRARIES = \ + libtestmodulea.la \ + libtestmoduleb.la + +libtestmodulea_la_SOURCES = test-module-a.c +libtestmodulea_la_LIBADD = $(LDADD) +libtestmodulea_la_LDFLAGS = $(LDFLAGS) -no-undefined -avoid-version + +libtestmoduleb_la_SOURCES = test-module-b.c +libtestmoduleb_la_LIBADD = $(LDADD) +libtestmoduleb_la_LDFLAGS =$(LDFLAGS) -no-undefined -avoid-version diff --git a/gio/tests/modules/test-module-a.c b/gio/tests/modules/test-module-a.c new file mode 100644 index 000000000..ffe7ae31e --- /dev/null +++ b/gio/tests/modules/test-module-a.c @@ -0,0 +1,57 @@ +/* Test module for GIOModule tests + * Copyright (C) 2013 Red Hat, Inc + * Author: Matthias Clasen + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + */ + +#include + +typedef struct _TestA { + GObject parent; +} TestA; + +typedef struct _TestAClass { + GObjectClass parent_class; +} TestAClass; + +G_DEFINE_TYPE (TestA, test_a, G_TYPE_OBJECT) + +static void +test_a_class_init (TestAClass *class) +{ +} + +static void +test_a_init (TestA *self) +{ +} + +void +g_io_module_load (GIOModule *module) +{ + g_io_extension_point_implement ("test-extension-point", + test_a_get_type (), + "test-a", + 30); +} + +void +g_io_module_unload (GIOModule *module) +{ +} diff --git a/gio/tests/modules/test-module-b.c b/gio/tests/modules/test-module-b.c new file mode 100644 index 000000000..aaf15d696 --- /dev/null +++ b/gio/tests/modules/test-module-b.c @@ -0,0 +1,57 @@ +/* Test module for GIOModule tests + * Copyright (C) 2013 Red Hat, Inc + * Author: Matthias Clasen + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + */ + +#include + +typedef struct _TestB { + GObject parent; +} TestB; + +typedef struct _TestBClass { + GObjectClass parent_class; +} TestBClass; + +G_DEFINE_TYPE (TestB, test_b, G_TYPE_OBJECT) + +static void +test_b_class_init (TestBClass *class) +{ +} + +static void +test_b_init (TestB *self) +{ +} + +void +g_io_module_load (GIOModule *module) +{ + g_io_extension_point_implement ("test-extension-point", + test_b_get_type (), + "test-b", + 40); +} + +void +g_io_module_unload (GIOModule *module) +{ +}