LCOV - code coverage report
Current view: top level - glib/girepository/tests - registered-type-info.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 32 32 100.0 %
Date: 2024-05-07 05:15:23 Functions: 2 2 100.0 %
Branches: 16 18 88.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2024 GNOME Foundation
       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
      17                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : 
      22                 :            : #include "girepository.h"
      23                 :            : #include "test-common.h"
      24                 :            : 
      25                 :            : static void
      26                 :          1 : test_boxed (RepositoryFixture *fx,
      27                 :            :             const void        *unused)
      28                 :            : {
      29                 :            :   const struct
      30                 :            :     {
      31                 :            :       const char *name;
      32                 :            :       GType expect_info_type;
      33                 :            :       gboolean expect_nonnull_gtype_info;
      34                 :            :       gboolean expect_is_gtype_struct;
      35                 :            :       gboolean expect_boxed;
      36                 :            :     }
      37                 :          7 :   types[] =
      38                 :            :     {
      39                 :            :       {
      40                 :            :         /* POD struct */
      41                 :            :         .name = "CClosure",
      42                 :          1 :         .expect_info_type = GI_TYPE_STRUCT_INFO,
      43                 :            :         .expect_nonnull_gtype_info = FALSE,
      44                 :            :         .expect_is_gtype_struct = FALSE,
      45                 :            :         .expect_boxed = FALSE,
      46                 :            :       },
      47                 :            :       {
      48                 :            :         /* POD union */
      49                 :            :         .name = "TypeCValue",
      50                 :          1 :         .expect_info_type = GI_TYPE_UNION_INFO,
      51                 :            :         .expect_nonnull_gtype_info = FALSE,
      52                 :            :         .expect_is_gtype_struct = FALSE,
      53                 :            :         .expect_boxed = FALSE,
      54                 :            :       },
      55                 :            :       {
      56                 :            :         /* struct for a different non-boxed GType */
      57                 :            :         .name = "InitiallyUnownedClass",
      58                 :          1 :         .expect_info_type = GI_TYPE_STRUCT_INFO,
      59                 :            :         .expect_nonnull_gtype_info = FALSE,
      60                 :            :         .expect_is_gtype_struct = TRUE,
      61                 :            :         .expect_boxed = FALSE,
      62                 :            :       },
      63                 :            :       {
      64                 :            :         /* boxed struct */
      65                 :            :         .name = "BookmarkFile",
      66                 :          1 :         .expect_info_type = GI_TYPE_STRUCT_INFO,
      67                 :            :         .expect_nonnull_gtype_info = TRUE,
      68                 :            :         .expect_is_gtype_struct = FALSE,
      69                 :            :         .expect_boxed = TRUE,
      70                 :            :       },
      71                 :            :       {
      72                 :            :         /* boxed struct */
      73                 :            :         .name = "Closure",
      74                 :          1 :         .expect_info_type = GI_TYPE_STRUCT_INFO,
      75                 :            :         .expect_nonnull_gtype_info = TRUE,
      76                 :            :         .expect_is_gtype_struct = FALSE,
      77                 :            :         .expect_boxed = TRUE,
      78                 :            :       },
      79                 :            :       {
      80                 :            :         /* non-boxed GType */
      81                 :            :         .name = "Object",
      82                 :          1 :         .expect_info_type = GI_TYPE_OBJECT_INFO,
      83                 :            :         .expect_nonnull_gtype_info = TRUE,
      84                 :            :         .expect_is_gtype_struct = FALSE,
      85                 :            :         .expect_boxed = FALSE,
      86                 :            :       },
      87                 :            :     };
      88                 :            : 
      89                 :          1 :   g_test_summary ("Test various boxed and non-boxed types for GIRegisteredTypeInfo");
      90                 :            : 
      91         [ +  + ]:          7 :   for (size_t i = 0; i < G_N_ELEMENTS (types); i++)
      92                 :            :     {
      93                 :          6 :       GIRegisteredTypeInfo *type_info = GI_REGISTERED_TYPE_INFO (gi_repository_find_by_name (fx->repository, "GObject", types[i].name));
      94                 :          6 :       g_assert_nonnull (type_info);
      95                 :            : 
      96         [ +  + ]:          6 :       g_test_message ("Expecting %s to %s", types[i].name, types[i].expect_boxed ? "be boxed" : "not be boxed");
      97                 :            : 
      98                 :          6 :       g_assert_cmpuint (G_TYPE_FROM_INSTANCE (type_info), ==, types[i].expect_info_type);
      99                 :            : 
     100         [ +  + ]:          6 :       if (types[i].expect_nonnull_gtype_info)
     101                 :            :         {
     102                 :          3 :           g_assert_nonnull (gi_registered_type_info_get_type_name (type_info));
     103                 :          3 :           g_assert_nonnull (gi_registered_type_info_get_type_init_function_name (type_info));
     104                 :            :         }
     105                 :            :       else
     106                 :            :         {
     107                 :          3 :           g_assert_null (gi_registered_type_info_get_type_name (type_info));
     108                 :          3 :           g_assert_null (gi_registered_type_info_get_type_init_function_name (type_info));
     109                 :            :         }
     110                 :            : 
     111   [ -  +  +  -  :          6 :       if (GI_IS_STRUCT_INFO (type_info))
             +  +  +  + ]
     112                 :            :         {
     113         [ +  + ]:          4 :           if (types[i].expect_is_gtype_struct)
     114                 :          1 :             g_assert_true (gi_struct_info_is_gtype_struct (GI_STRUCT_INFO (type_info)));
     115                 :            :           else
     116                 :          3 :             g_assert_false (gi_struct_info_is_gtype_struct (GI_STRUCT_INFO (type_info)));
     117                 :            :         }
     118                 :            : 
     119         [ +  + ]:          6 :       if (types[i].expect_boxed)
     120                 :          2 :         g_assert_true (gi_registered_type_info_is_boxed (type_info));
     121                 :            :       else
     122                 :          4 :         g_assert_false (gi_registered_type_info_is_boxed (type_info));
     123                 :            : 
     124                 :          6 :       g_clear_pointer (&type_info, gi_base_info_unref);
     125                 :            :     }
     126                 :          1 : }
     127                 :            : 
     128                 :            : int
     129                 :          1 : main (int argc,
     130                 :            :       char *argv[])
     131                 :            : {
     132                 :          1 :   repository_init (&argc, &argv);
     133                 :            : 
     134                 :          1 :   ADD_REPOSITORY_TEST ("/registered-type-info/boxed", test_boxed, &typelib_load_spec_gobject);
     135                 :            : 
     136                 :          1 :   return g_test_run ();
     137                 :            : }

Generated by: LCOV version 1.14