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

           Branch data     Line data    Source code
       1                 :            : /* GIO default value tests
       2                 :            :  * Copyright (C) 2013 Red Hat, Inc.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library 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.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General Public License
      17                 :            :  * along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : 
      22                 :            : #include <string.h>
      23                 :            : #include <gio/gio.h>
      24                 :            : 
      25                 :            : #ifdef HAVE_COCOA
      26                 :            : #include <gio/gosxappinfo.h>
      27                 :            : #endif
      28                 :            : 
      29                 :            : static void
      30                 :         73 : check_property (const char *output,
      31                 :            :                 GParamSpec *pspec,
      32                 :            :                 GValue *value)
      33                 :            : {
      34                 :         73 :   GValue default_value = G_VALUE_INIT;
      35                 :            :   char *v, *dv, *msg;
      36                 :            : 
      37         [ +  - ]:         73 :   if (g_param_value_defaults (pspec, value))
      38                 :         73 :       return;
      39                 :            : 
      40                 :          0 :   g_param_value_set_default (pspec, &default_value);
      41                 :            : 
      42                 :          0 :   v = g_strdup_value_contents (value);
      43                 :          0 :   dv = g_strdup_value_contents (&default_value);
      44                 :            : 
      45                 :          0 :   msg = g_strdup_printf ("%s %s.%s: %s != %s\n",
      46                 :            :                          output,
      47                 :            :                          g_type_name (pspec->owner_type),
      48                 :            :                          pspec->name,
      49                 :            :                          dv, v);
      50                 :          0 :   g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
      51                 :          0 :   g_free (msg);
      52                 :            : 
      53                 :          0 :   g_free (v);
      54                 :          0 :   g_free (dv);
      55                 :          0 :   g_value_unset (&default_value);
      56                 :            : }
      57                 :            : 
      58                 :            : static void
      59                 :         84 : test_type (gconstpointer data)
      60                 :            : {
      61                 :            :   GObjectClass *klass;
      62                 :            :   GObject *instance;
      63                 :            :   GParamSpec **pspecs;
      64                 :            :   guint n_pspecs, i;
      65                 :            :   GType type;
      66                 :            : 
      67                 :         84 :   type = * (GType *) data;
      68                 :            : 
      69   [ +  +  -  + ]:         84 :   if (g_type_is_a (type, G_TYPE_APP_INFO_MONITOR))
      70                 :            :     {
      71                 :          1 :       g_test_skip ("singleton");
      72                 :         33 :       return;
      73                 :            :     }
      74                 :            : 
      75   [ +  +  +  - ]:         83 :   if (g_type_is_a (type, G_TYPE_BINDING) ||
      76   [ +  +  +  + ]:         82 :       g_type_is_a (type, G_TYPE_BUFFERED_INPUT_STREAM) ||
      77   [ +  +  +  - ]:         80 :       g_type_is_a (type, G_TYPE_BUFFERED_OUTPUT_STREAM) ||
      78   [ +  +  +  - ]:         79 :       g_type_is_a (type, G_TYPE_CHARSET_CONVERTER) ||
      79   [ +  +  +  - ]:         78 :       g_type_is_a (type, G_TYPE_DBUS_ACTION_GROUP) ||
      80   [ +  +  +  - ]:         77 :       g_type_is_a (type, G_TYPE_DBUS_CONNECTION) ||
      81   [ +  +  +  - ]:         76 :       g_type_is_a (type, G_TYPE_DBUS_OBJECT_MANAGER_CLIENT) ||
      82   [ +  +  +  - ]:         75 :       g_type_is_a (type, G_TYPE_DBUS_OBJECT_MANAGER_SERVER) ||
      83   [ +  +  +  - ]:         74 :       g_type_is_a (type, G_TYPE_DBUS_PROXY) ||
      84   [ +  +  +  - ]:         73 :       g_type_is_a (type, G_TYPE_DBUS_SERVER) ||
      85   [ +  -  +  + ]:         72 :       g_type_is_a (type, G_TYPE_FILTER_OUTPUT_STREAM) ||
      86   [ +  -  +  + ]:         70 :       g_type_is_a (type, G_TYPE_FILTER_INPUT_STREAM) ||
      87   [ +  +  +  - ]:         69 :       g_type_is_a (type, G_TYPE_INET_ADDRESS) ||
      88   [ +  +  +  + ]:         68 :       g_type_is_a (type, G_TYPE_INET_SOCKET_ADDRESS) ||
      89   [ +  +  +  -  :        131 :       g_type_is_a (type, G_TYPE_PROPERTY_ACTION) ||
                   +  + ]
      90         [ +  - ]:        129 :       g_type_is_a (type, G_TYPE_SETTINGS) ||
      91   [ +  +  +  + ]:         64 :       g_type_is_a (type, G_TYPE_SOCKET_CONNECTION) ||
      92   [ +  +  +  - ]:         60 :       g_type_is_a (type, G_TYPE_SIMPLE_IO_STREAM) ||
      93   [ +  +  -  + ]:         59 :       g_type_is_a (type, G_TYPE_THEMED_ICON) ||
      94                 :            :       FALSE)
      95                 :            :     {
      96                 :         25 :       g_test_skip ("mandatory construct params");
      97                 :         25 :       return;
      98                 :            :     }
      99                 :            : 
     100   [ +  +  +  - ]:         58 :   if (g_type_is_a (type, G_TYPE_DBUS_MENU_MODEL) ||
     101   [ +  +  -  + ]:         57 :       g_type_is_a (type, G_TYPE_DBUS_METHOD_INVOCATION))
     102                 :            :     {
     103                 :          2 :       g_test_skip ("crash in finalize");
     104                 :          2 :       return;
     105                 :            :     }
     106                 :            : 
     107   [ +  +  +  - ]:         56 :   if (g_type_is_a (type, G_TYPE_FILE_ENUMERATOR) ||
     108   [ +  +  -  + ]:         55 :       g_type_is_a (type, G_TYPE_FILE_IO_STREAM))
     109                 :            :     {
     110                 :          2 :       g_test_skip ("should be abstract");
     111                 :          2 :       return;
     112                 :            :     }
     113                 :            : 
     114                 :         54 :   klass = g_type_class_ref (type);
     115                 :         54 :   instance = g_object_new (type, NULL);
     116                 :            : 
     117   [ -  +  +  -  :         57 :   if (G_IS_INITABLE (instance) &&
          -  +  +  +  +  
                      - ]
     118                 :          3 :       !g_initable_init (G_INITABLE (instance), NULL, NULL))
     119                 :            :     {
     120                 :          3 :       g_test_skip ("initialization failed");
     121                 :          3 :       g_object_unref (instance);
     122                 :          3 :       g_type_class_unref (klass);
     123                 :          3 :       return;
     124                 :            :     }
     125                 :            : 
     126   [ +  +  -  + ]:         51 :   if (g_type_is_a (type, G_TYPE_INITIALLY_UNOWNED))
     127                 :          1 :     g_object_ref_sink (instance);
     128                 :            : 
     129                 :         51 :   pspecs = g_object_class_list_properties (klass, &n_pspecs);
     130         [ +  + ]:        139 :   for (i = 0; i < n_pspecs; ++i)
     131                 :            :     {
     132                 :         88 :       GParamSpec *pspec = pspecs[i];
     133                 :         88 :       GValue value = G_VALUE_INIT;
     134                 :            : 
     135         [ +  + ]:         88 :       if (pspec->owner_type != type)
     136                 :         15 :         continue;
     137                 :            : 
     138         [ +  + ]:         81 :       if ((pspec->flags & G_PARAM_READABLE) == 0)
     139                 :          5 :         continue;
     140                 :            : 
     141   [ +  +  -  + ]:         76 :       if (g_type_is_a (type, G_TYPE_APPLICATION) &&
     142         [ +  + ]:          8 :           (strcmp (pspec->name, "is-remote") == 0))
     143                 :            :         {
     144                 :          1 :           g_test_message ("skipping GApplication:is-remote");
     145                 :          1 :           continue;
     146                 :            :         }
     147                 :            : 
     148   [ +  +  -  + ]:         75 :       if (g_type_is_a (type, G_TYPE_PROXY_ADDRESS_ENUMERATOR) &&
     149         [ +  + ]:          4 :           (strcmp (pspec->name, "proxy-resolver") == 0))
     150                 :            :         {
     151                 :          1 :           g_test_message ("skipping GProxyAddressEnumerator:proxy-resolver");
     152                 :          1 :           continue;
     153                 :            :         }
     154                 :            : 
     155   [ +  +  -  + ]:         74 :       if (g_type_is_a (type, G_TYPE_SOCKET_CLIENT) &&
     156         [ +  + ]:          9 :           (strcmp (pspec->name, "proxy-resolver") == 0))
     157                 :            :         {
     158                 :          1 :           g_test_message ("skipping GSocketClient:proxy-resolver");
     159                 :          1 :           continue;
     160                 :            :         }
     161                 :            : 
     162         [ -  + ]:         73 :       if (g_test_verbose ())
     163                 :          0 :         g_printerr ("Property %s.%s\n", g_type_name (pspec->owner_type), pspec->name);
     164                 :         73 :       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
     165                 :         73 :       g_object_get_property (instance, pspec->name, &value);
     166                 :         73 :       check_property ("Property", pspec, &value);
     167                 :         73 :       g_value_unset (&value);
     168                 :            :     }
     169                 :         51 :   g_free (pspecs);
     170                 :         51 :   g_object_unref (instance);
     171                 :         51 :   g_type_class_unref (klass);
     172                 :            : }
     173                 :            : 
     174                 :            : static GType *all_registered_types;
     175                 :            : 
     176                 :            : static const GType *
     177                 :          1 : list_all_types (void)
     178                 :            : {
     179         [ +  - ]:          1 :   if (!all_registered_types)
     180                 :            :     {
     181                 :            :       GType *tp;
     182                 :          1 :       all_registered_types = g_new0 (GType, 1000);
     183                 :          1 :       tp = all_registered_types;
     184                 :            : #include "giotypefuncs.inc"
     185                 :          1 :       *tp = 0;
     186                 :            :     }
     187                 :            : 
     188                 :          1 :   return all_registered_types;
     189                 :            : }
     190                 :            : 
     191                 :            : int
     192                 :          1 : main (int argc, char **argv)
     193                 :            : {
     194                 :            :   const GType *otypes;
     195                 :            :   guint i;
     196                 :            :   GTestDBus *bus;
     197                 :            :   gint result;
     198                 :            : 
     199                 :          1 :   g_setenv ("GIO_USE_VFS", "local", TRUE);
     200                 :          1 :   g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
     201                 :            : 
     202                 :            :   /* Disable deprecation warnings when we poke at deprecated properties */
     203                 :          1 :   g_setenv ("G_ENABLE_DIAGNOSTIC", "0", TRUE);
     204                 :            : 
     205                 :          1 :   g_test_init (&argc, &argv, NULL);
     206                 :            : 
     207                 :            :   /* Create one test bus for all tests, as we have a lot of very small
     208                 :            :    * and quick tests.
     209                 :            :    */
     210                 :          1 :   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
     211                 :          1 :   g_test_dbus_up (bus);
     212                 :            : 
     213                 :          1 :   otypes = list_all_types ();
     214         [ +  + ]:        279 :   for (i = 0; otypes[i]; i++)
     215                 :            :     {
     216                 :            :       gchar *testname;
     217                 :            : 
     218         [ +  + ]:        278 :       if (!G_TYPE_IS_CLASSED (otypes[i]))
     219                 :         90 :         continue;
     220                 :            : 
     221         [ +  + ]:        188 :       if (G_TYPE_IS_ABSTRACT (otypes[i]))
     222                 :         20 :         continue;
     223                 :            : 
     224   [ +  +  +  + ]:        168 :       if (!g_type_is_a (otypes[i], G_TYPE_OBJECT))
     225                 :         84 :         continue;
     226                 :            : 
     227                 :         84 :       testname = g_strdup_printf ("/Default Values/%s",
     228                 :         84 :                                   g_type_name (otypes[i]));
     229                 :         84 :       g_test_add_data_func (testname, &otypes[i], test_type);
     230                 :         84 :       g_free (testname);
     231                 :            :     }
     232                 :            : 
     233                 :          1 :   result = g_test_run ();
     234                 :            : 
     235                 :          1 :   g_test_dbus_down (bus);
     236                 :          1 :   g_object_unref (bus);
     237                 :            : 
     238                 :          1 :   return result;
     239                 :            : }

Generated by: LCOV version 1.14