LCOV - code coverage report
Current view: top level - glib/gio/tests - giomodule.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 62 62 100.0 %
Date: 2024-04-23 05:16:05 Functions: 4 4 100.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* Unit tests for GIOModule
       2                 :            :  * Copyright (C) 2013 Red Hat, Inc
       3                 :            :  * Author: Matthias Clasen
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       7                 :            :  * This work is provided "as is"; redistribution and modification
       8                 :            :  * in whole or in part, in any medium, physical or electronic is
       9                 :            :  * permitted without restriction.
      10                 :            :  *
      11                 :            :  * This work is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      14                 :            :  *
      15                 :            :  * In no event shall the authors or contributors be liable for any
      16                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      17                 :            :  * damages (including, but not limited to, procurement of substitute
      18                 :            :  * goods or services; loss of use, data, or profits; or business
      19                 :            :  * interruption) however caused and on any theory of liability, whether
      20                 :            :  * in contract, strict liability, or tort (including negligence or
      21                 :            :  * otherwise) arising in any way out of the use of this software, even
      22                 :            :  * if advised of the possibility of such damage.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <gio/gio.h>
      26                 :            : #include <glibconfig.h>
      27                 :            : 
      28                 :            : #ifdef G_OS_WIN32
      29                 :            :   #ifdef _MSC_VER
      30                 :            :     #define MODULE_FILENAME(x) "" x ".dll"
      31                 :            :   #else
      32                 :            :     #define MODULE_FILENAME(x) "lib" x ".dll"
      33                 :            :   #endif
      34                 :            : #elif defined(__APPLE__)
      35                 :            :   #define MODULE_FILENAME(x) "lib" x ".dylib"
      36                 :            : #else
      37                 :            :   #define MODULE_FILENAME(x) "lib" x ".so"
      38                 :            : #endif
      39                 :            : 
      40                 :            : static void
      41                 :          1 : test_extension_point (void)
      42                 :            : {
      43                 :            :   GIOExtensionPoint *ep, *ep2;
      44                 :            :   GIOExtension *ext;
      45                 :            :   GList *list;
      46                 :            :   GType req;
      47                 :            :   GTypeClass *class;
      48                 :            : 
      49                 :          1 :   ep = g_io_extension_point_lookup ("test-extension-point");
      50                 :          1 :   g_assert_null (ep);
      51                 :          1 :   ep = g_io_extension_point_register ("test-extension-point");
      52                 :          1 :   ep2 = g_io_extension_point_lookup ("test-extension-point");
      53                 :          1 :   g_assert (ep2 == ep);
      54                 :            : 
      55                 :          1 :   req = g_io_extension_point_get_required_type (ep);
      56                 :          1 :   g_assert (req == G_TYPE_INVALID);
      57                 :          1 :   g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
      58                 :          1 :   req = g_io_extension_point_get_required_type (ep);
      59                 :          1 :   g_assert (req == G_TYPE_OBJECT);
      60                 :            : 
      61                 :          1 :   list = g_io_extension_point_get_extensions (ep);
      62                 :          1 :   g_assert_null (list);
      63                 :            : 
      64                 :          1 :   g_io_extension_point_implement ("test-extension-point",
      65                 :            :                                   G_TYPE_VFS,
      66                 :            :                                   "extension1",
      67                 :            :                                   10);
      68                 :            : 
      69                 :          1 :   g_io_extension_point_implement ("test-extension-point",
      70                 :            :                                   G_TYPE_OBJECT,
      71                 :            :                                   "extension2",
      72                 :            :                                   20);
      73                 :            : 
      74                 :          1 :   list = g_io_extension_point_get_extensions (ep);
      75                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
      76                 :            :   
      77                 :          1 :   ext = list->data;
      78                 :          1 :   g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2");
      79                 :          1 :   g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT);
      80                 :          1 :   g_assert (g_io_extension_get_priority (ext) == 20);
      81                 :          1 :   class = g_io_extension_ref_class (ext);
      82                 :          1 :   g_assert (class == g_type_class_peek (G_TYPE_OBJECT));
      83                 :          1 :   g_type_class_unref (class);
      84                 :            : 
      85                 :          1 :   ext = list->next->data;
      86                 :          1 :   g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1");
      87                 :          1 :   g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS);
      88                 :          1 :   g_assert (g_io_extension_get_priority (ext) == 10);
      89                 :          1 : }
      90                 :            : 
      91                 :            : #define INHERIT_ALL (G_TEST_SUBPROCESS_INHERIT_STDIN | \
      92                 :            :                      G_TEST_SUBPROCESS_INHERIT_STDOUT | \
      93                 :            :                      G_TEST_SUBPROCESS_INHERIT_STDERR)
      94                 :            : 
      95                 :            : static void
      96                 :          2 : test_module_scan_all (void)
      97                 :            : {
      98                 :            : #ifdef GLIB_STATIC_COMPILATION
      99                 :            :   /* The plugin module is statically linked with a separate copy
     100                 :            :    * of GLib so g_io_extension_point_implement won't work. */
     101                 :            :   g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
     102                 :            :   return;
     103                 :            : #endif
     104                 :            : 
     105         [ +  + ]:          2 :   if (g_test_subprocess ())
     106                 :            :     {
     107                 :            :       GIOExtensionPoint *ep;
     108                 :            :       GIOExtension *ext;
     109                 :            :       GList *list;
     110                 :          1 :       ep = g_io_extension_point_register ("test-extension-point");
     111                 :          1 :       g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_BUILT, "modules", NULL));
     112                 :          1 :       list = g_io_extension_point_get_extensions (ep);
     113                 :          1 :       g_assert_cmpint (g_list_length (list), ==, 2);
     114                 :          1 :       ext = list->data;
     115                 :          1 :       g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b");
     116                 :          1 :       ext = list->next->data;
     117                 :          1 :       g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
     118                 :          1 :       return;
     119                 :            :     }
     120                 :          1 :   g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
     121                 :          1 :   g_test_trap_assert_passed ();
     122                 :            : }
     123                 :            : 
     124                 :            : static void
     125                 :          2 : test_module_scan_all_with_scope (void)
     126                 :            : {
     127                 :            : #ifdef GLIB_STATIC_COMPILATION
     128                 :            :   /* Disabled for the same reason as test_module_scan_all. */
     129                 :            :   g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
     130                 :            :   return;
     131                 :            : #endif
     132                 :            : 
     133         [ +  + ]:          2 :   if (g_test_subprocess ())
     134                 :            :     {
     135                 :            :       GIOExtensionPoint *ep;
     136                 :            :       GIOModuleScope *scope;
     137                 :            :       GIOExtension *ext;
     138                 :            :       GList *list;
     139                 :            : 
     140                 :          1 :       ep = g_io_extension_point_register ("test-extension-point");
     141                 :          1 :       scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
     142                 :          1 :       g_io_module_scope_block (scope, MODULE_FILENAME ("testmoduleb"));
     143                 :          1 :       g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_BUILT, "modules", NULL), scope);
     144                 :          1 :       list = g_io_extension_point_get_extensions (ep);
     145                 :          1 :       g_assert_cmpint (g_list_length (list), ==, 1);
     146                 :          1 :       ext = list->data;
     147                 :          1 :       g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
     148                 :          1 :       g_io_module_scope_free (scope);
     149                 :          1 :       return;
     150                 :            :     }
     151                 :          1 :   g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
     152                 :          1 :   g_test_trap_assert_passed ();
     153                 :            : }
     154                 :            : 
     155                 :            : int
     156                 :          3 : main (int argc, char *argv[])
     157                 :            : {
     158                 :          3 :   g_test_init (&argc, &argv, NULL);
     159                 :            : 
     160                 :          3 :   g_test_add_func ("/giomodule/extension-point", test_extension_point);
     161                 :          3 :   g_test_add_func ("/giomodule/module-scan-all", test_module_scan_all);
     162                 :          3 :   g_test_add_func ("/giomodule/module-scan-all-with-scope", test_module_scan_all_with_scope);
     163                 :            : 
     164                 :          3 :   return g_test_run ();
     165                 :            : }

Generated by: LCOV version 1.14