LCOV - code coverage report
Current view: top level - gio/tests - file-enumerator.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 27 27
Test Date: 2025-11-11 05:25:45 Functions: 100.0 % 8 8
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLib testing framework examples and tests
       2                 :             :  *
       3                 :             :  * Copyright 2025 GNOME Foundation, Inc.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General
      18                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include <gio/gio.h>
      22                 :             : 
      23                 :             : #define TYPE_TEST_FILE_ENUMERATOR (test_file_enumerator_get_type ())
      24                 :             : #define TEST_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TYPE_TEST_FILE_ENUMERATOR, TestFileEnumerator))
      25                 :             : #define IS_TEST_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TYPE_TEST_FILE_ENUMERATOR))
      26                 :             : 
      27                 :             : typedef struct
      28                 :             : {
      29                 :             :   GFileEnumerator parent_instance;
      30                 :             :   unsigned int n_times_closed; // Number of times the enumerator has been closed
      31                 :             : } TestFileEnumerator;
      32                 :             : 
      33                 :             : typedef struct
      34                 :             : {
      35                 :             :   GFileEnumeratorClass parent_class;
      36                 :             : } TestFileEnumeratorClass;
      37                 :             : 
      38                 :             : GType test_file_enumerator_get_type (void) G_GNUC_CONST;
      39                 :             : 
      40                 :           4 : G_DEFINE_TYPE (TestFileEnumerator, test_file_enumerator, G_TYPE_FILE_ENUMERATOR)
      41                 :             : 
      42                 :             : static gboolean
      43                 :           1 : test_file_enumerator_close (GFileEnumerator *enumerator,
      44                 :             :                             GCancellable *cancellable,
      45                 :             :                             GError **error)
      46                 :             : {
      47                 :           1 :   TestFileEnumerator *test = TEST_FILE_ENUMERATOR (enumerator);
      48                 :           1 :   ++test->n_times_closed;
      49                 :           1 :   return TRUE;
      50                 :             : }
      51                 :             : 
      52                 :             : static void
      53                 :           1 : test_file_enumerator_init (TestFileEnumerator *enumerator)
      54                 :             : {
      55                 :           1 :   enumerator->n_times_closed = 0;
      56                 :           1 : }
      57                 :             : 
      58                 :             : static void
      59                 :           1 : test_file_enumerator_class_init (TestFileEnumeratorClass *klass)
      60                 :             : {
      61                 :             :   GFileEnumeratorClass *enumerator_class;
      62                 :             : 
      63                 :           1 :   enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
      64                 :           1 :   enumerator_class->close_fn = test_file_enumerator_close;
      65                 :           1 : }
      66                 :             : 
      67                 :             : static void
      68                 :           1 : test_close_on_dispose (void)
      69                 :             : {
      70                 :             :   GFile *dir;
      71                 :             :   TestFileEnumerator *enumerator;
      72                 :             : 
      73                 :           1 :   dir = g_file_new_for_path (g_get_tmp_dir ());
      74                 :             : 
      75                 :           1 :   enumerator = g_object_new (TYPE_TEST_FILE_ENUMERATOR,
      76                 :             :                              "container", dir,
      77                 :             :                              NULL);
      78                 :             : 
      79                 :             :   // Check enumerator is not closed initially
      80                 :           1 :   g_assert_cmpuint (enumerator->n_times_closed, ==, 0);
      81                 :             : 
      82                 :           1 :   g_object_run_dispose (G_OBJECT (enumerator));
      83                 :             : 
      84                 :             :   // Check enumerator is closed after 1st dispose
      85                 :           1 :   g_assert_cmpuint (enumerator->n_times_closed, ==, 1);
      86                 :             : 
      87                 :           1 :   g_object_run_dispose (G_OBJECT (enumerator));
      88                 :             : 
      89                 :             :   // Check enumerator is not closed twice after 2nd dispose
      90                 :           1 :   g_assert_cmpuint (enumerator->n_times_closed, ==, 1);
      91                 :             : 
      92                 :           1 :   g_object_unref (enumerator);
      93                 :           1 :   g_object_unref (dir);
      94                 :           1 : }
      95                 :             : 
      96                 :             : int
      97                 :           1 : main (int argc, char *argv[])
      98                 :             : {
      99                 :           1 :   g_test_init (&argc, &argv, NULL);
     100                 :             : 
     101                 :           1 :   g_test_add_func ("/file-enumerator/close-on-dispose", test_close_on_dispose);
     102                 :           1 :   return g_test_run ();
     103                 :             : }
        

Generated by: LCOV version 2.0-1