LCOV - code coverage report
Current view: top level - glib/glib/tests - testing-helper.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 124 130 95.4 %
Date: 2024-04-23 05:16:05 Functions: 13 14 92.9 %
Branches: 41 44 93.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2018 Collabora Ltd.
       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
      18                 :            :  * <http://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <glib.h>
      22                 :            : #include <locale.h>
      23                 :            : #include <stdio.h>
      24                 :            : #ifdef G_OS_WIN32
      25                 :            : #include <fcntl.h>
      26                 :            : #include <io.h>
      27                 :            : #include <stdio.h>
      28                 :            : #endif
      29                 :            : 
      30                 :            : static void
      31                 :         92 : test_pass (void)
      32                 :            : {
      33                 :         92 : }
      34                 :            : 
      35                 :            : static void
      36                 :          6 : test_skip (void)
      37                 :            : {
      38                 :          6 :   g_test_skip ("not enough tea");
      39                 :          6 : }
      40                 :            : 
      41                 :            : static void
      42                 :          2 : test_skip_printf (void)
      43                 :            : {
      44                 :          2 :   const char *beverage = "coffee";
      45                 :            : 
      46                 :          2 :   g_test_skip_printf ("not enough %s", beverage);
      47                 :          2 : }
      48                 :            : 
      49                 :            : static void
      50                 :          4 : test_fail (void)
      51                 :            : {
      52                 :          4 :   g_test_fail ();
      53                 :          4 : }
      54                 :            : 
      55                 :            : static void
      56                 :          0 : test_error (void)
      57                 :            : {
      58                 :            :   /* We expect this test to abort, so try to avoid that creating a coredump */
      59                 :          0 :   g_test_disable_crash_reporting ();
      60                 :            : 
      61                 :          0 :   g_error ("This should error out\nBecause it's just\nwrong!");
      62                 :            : }
      63                 :            : 
      64                 :            : static void
      65                 :          2 : test_fail_printf (void)
      66                 :            : {
      67                 :          2 :   g_test_fail_printf ("this test intentionally left failing");
      68                 :          2 : }
      69                 :            : 
      70                 :            : static void
      71                 :          6 : test_incomplete (void)
      72                 :            : {
      73                 :          6 :   g_test_incomplete ("mind reading not implemented yet");
      74                 :          6 : }
      75                 :            : 
      76                 :            : static void
      77                 :          2 : test_incomplete_printf (void)
      78                 :            : {
      79                 :          2 :   const char *operation = "telekinesis";
      80                 :            : 
      81                 :          2 :   g_test_incomplete_printf ("%s not implemented yet", operation);
      82                 :          2 : }
      83                 :            : 
      84                 :            : static void
      85                 :          2 : test_summary (void)
      86                 :            : {
      87                 :          2 :   g_test_summary ("Tests that g_test_summary() works with TAP, by outputting a "
      88                 :            :                   "known summary message in testing-helper, and checking for "
      89                 :            :                   "it in the TAP output later.");
      90                 :          2 : }
      91                 :            : 
      92                 :            : static void
      93                 :          2 : test_message (void)
      94                 :            : {
      95                 :          2 :   g_test_message ("Tests that single line message works");
      96                 :          2 :   g_test_message ("Tests that multi\n\nline\nmessage\nworks");
      97                 :          2 :   g_test_message ("\nTests that multi\nline\nmessage\nworks with leading and trailing too\n");
      98                 :          2 : }
      99                 :            : 
     100                 :            : static void
     101                 :          2 : test_print (void)
     102                 :            : {
     103                 :          2 :   g_print ("Tests that single line message works\n");
     104                 :          2 :   g_print ("test that multiple\nlines ");
     105                 :          2 :   g_print ("can be ");
     106                 :          2 :   g_print ("written ");
     107                 :          2 :   g_print ("separately\n");
     108                 :          2 : }
     109                 :            : 
     110                 :            : static void
     111                 :          2 : test_subprocess_stdout (void)
     112                 :            : {
     113         [ +  + ]:          2 :   if (g_test_subprocess ())
     114                 :            :     {
     115                 :          1 :       printf ("Tests that single line message works\n");
     116                 :          1 :       printf ("test that multiple\nlines ");
     117                 :          1 :       printf ("can be ");
     118                 :          1 :       printf ("written ");
     119                 :          1 :       printf ("separately\n");
     120                 :            : 
     121                 :          1 :       puts ("And another line has been put");
     122                 :            : 
     123                 :          1 :       return;
     124                 :            :     }
     125                 :            : 
     126                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
     127                 :          1 :   g_test_trap_has_passed ();
     128                 :            : 
     129                 :          1 :   g_test_trap_assert_stdout ("/sub-stdout: Tests that single line message works\n*");
     130                 :          1 :   g_test_trap_assert_stdout ("*\ntest that multiple\nlines can be written separately\n*");
     131                 :          1 :   g_test_trap_assert_stdout ("*\nAnd another line has been put\n*");
     132                 :            : }
     133                 :            : 
     134                 :            : static void
     135                 :          2 : test_subprocess_stdout_no_nl (void)
     136                 :            : {
     137         [ +  + ]:          2 :   if (g_test_subprocess ())
     138                 :            :     {
     139                 :          1 :       printf ("A message without trailing new line");
     140                 :          1 :       return;
     141                 :            :     }
     142                 :            : 
     143                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
     144                 :          1 :   g_test_trap_has_passed ();
     145                 :            : 
     146                 :          1 :   g_test_trap_assert_stdout ("/sub-stdout-no-nl: A message without trailing new line");
     147                 :            : }
     148                 :            : 
     149                 :            : int
     150                 :         48 : main (int   argc,
     151                 :            :       char *argv[])
     152                 :            : {
     153                 :            :   char *argv1;
     154                 :            : 
     155                 :         48 :   setlocale (LC_ALL, "");
     156                 :            : 
     157                 :            : #ifdef G_OS_WIN32
     158                 :            :   /* Windows opens std streams in text mode, with \r\n EOLs.
     159                 :            :    * Sometimes it's easier to force a switch to binary mode than
     160                 :            :    * to account for extra \r in testcases.
     161                 :            :    */
     162                 :            :   setmode (fileno (stdout), O_BINARY);
     163                 :            : #endif
     164                 :            : 
     165                 :         48 :   g_return_val_if_fail (argc > 1, 1);
     166                 :         48 :   argv1 = argv[1];
     167                 :            : 
     168         [ +  + ]:         48 :   if (argc > 2)
     169                 :         47 :     memmove (&argv[1], &argv[2], (argc - 2) * sizeof (char *));
     170                 :            : 
     171                 :         48 :   argc -= 1;
     172                 :         48 :   argv[argc] = NULL;
     173                 :            : 
     174         [ +  + ]:         48 :   if (g_strcmp0 (argv1, "init-null-argv0") == 0)
     175                 :            :     {
     176                 :          1 :       int test_argc = 0;
     177                 :          1 :       char *test_argva[1] = { NULL };
     178                 :          1 :       char **test_argv = test_argva;
     179                 :            : 
     180                 :            :       /* Test that `g_test_init()` can handle being called with an empty argv
     181                 :            :        * and argc == 0. While this isn’t recommended, it is possible for another
     182                 :            :        * process to use execve() to call a gtest process this way, so we’d
     183                 :            :        * better handle it gracefully.
     184                 :            :        *
     185                 :            :        * This test can’t be run after `g_test_init()` has been called normally,
     186                 :            :        * as it isn’t allowed to be called more than once in a process. */
     187                 :          1 :       g_test_init (&test_argc, &test_argv, NULL);
     188                 :            : 
     189                 :          1 :       return 0;
     190                 :            :     }
     191                 :            : 
     192                 :         47 :   g_test_init (&argc, &argv, NULL);
     193                 :         44 :   g_test_set_nonfatal_assertions ();
     194                 :            : 
     195         [ +  + ]:         44 :   if (g_strcmp0 (argv1, "pass") == 0)
     196                 :            :     {
     197                 :          2 :       g_test_add_func ("/pass", test_pass);
     198                 :            :     }
     199         [ +  + ]:         42 :   else if (g_strcmp0 (argv1, "skip") == 0)
     200                 :            :     {
     201                 :          2 :       g_test_add_func ("/skip", test_skip);
     202                 :            :     }
     203         [ +  + ]:         40 :   else if (g_strcmp0 (argv1, "skip-printf") == 0)
     204                 :            :     {
     205                 :          2 :       g_test_add_func ("/skip-printf", test_skip_printf);
     206                 :            :     }
     207         [ +  + ]:         38 :   else if (g_strcmp0 (argv1, "incomplete") == 0)
     208                 :            :     {
     209                 :          2 :       g_test_add_func ("/incomplete", test_incomplete);
     210                 :            :     }
     211         [ +  + ]:         36 :   else if (g_strcmp0 (argv1, "incomplete-printf") == 0)
     212                 :            :     {
     213                 :          2 :       g_test_add_func ("/incomplete-printf", test_incomplete_printf);
     214                 :            :     }
     215         [ +  + ]:         34 :   else if (g_strcmp0 (argv1, "fail") == 0)
     216                 :            :     {
     217                 :          2 :       g_test_add_func ("/fail", test_fail);
     218                 :            :     }
     219         [ -  + ]:         32 :   else if (g_strcmp0 (argv1, "error") == 0)
     220                 :            :     {
     221                 :          0 :       g_test_add_func ("/error", test_error);
     222                 :            :     }
     223         [ -  + ]:         32 :   else if (g_strcmp0 (argv1, "error-and-pass") == 0)
     224                 :            :     {
     225                 :          0 :       g_test_add_func ("/error", test_error);
     226                 :          0 :       g_test_add_func ("/pass", test_pass);
     227                 :            :     }
     228         [ +  + ]:         32 :   else if (g_strcmp0 (argv1, "fail-printf") == 0)
     229                 :            :     {
     230                 :          2 :       g_test_add_func ("/fail-printf", test_fail_printf);
     231                 :            :     }
     232         [ +  + ]:         30 :   else if (g_strcmp0 (argv1, "all-non-failures") == 0)
     233                 :            :     {
     234                 :          2 :       g_test_add_func ("/pass", test_pass);
     235                 :          2 :       g_test_add_func ("/skip", test_skip);
     236                 :          2 :       g_test_add_func ("/incomplete", test_incomplete);
     237                 :            :     }
     238         [ +  + ]:         28 :   else if (g_strcmp0 (argv1, "all") == 0)
     239                 :            :     {
     240                 :          2 :       g_test_add_func ("/pass", test_pass);
     241                 :          2 :       g_test_add_func ("/skip", test_skip);
     242                 :          2 :       g_test_add_func ("/incomplete", test_incomplete);
     243                 :          2 :       g_test_add_func ("/fail", test_fail);
     244                 :            :     }
     245         [ +  + ]:         26 :   else if (g_strcmp0 (argv1, "skip-options") == 0)
     246                 :            :     {
     247                 :            :       /* The caller is expected to skip some of these with
     248                 :            :        * -p/-r, -s/-x and/or --GTestSkipCount */
     249                 :         16 :       g_test_add_func ("/a", test_pass);
     250                 :         16 :       g_test_add_func ("/b", test_pass);
     251                 :         16 :       g_test_add_func ("/b/a", test_pass);
     252                 :         16 :       g_test_add_func ("/b/b", test_pass);
     253                 :         16 :       g_test_add_func ("/b/b/a", test_pass);
     254                 :         16 :       g_test_add_func ("/prefix/a", test_pass);
     255                 :         16 :       g_test_add_func ("/prefix/b/b", test_pass);
     256                 :         16 :       g_test_add_func ("/prefix-long/a", test_pass);
     257                 :         16 :       g_test_add_func ("/c/a", test_pass);
     258                 :         16 :       g_test_add_func ("/d/a", test_pass);
     259                 :            :     }
     260         [ +  + ]:         10 :   else if (g_strcmp0 (argv1, "summary") == 0)
     261                 :            :     {
     262                 :          2 :       g_test_add_func ("/summary", test_summary);
     263                 :            :     }
     264         [ +  + ]:          8 :   else if (g_strcmp0 (argv1, "message") == 0)
     265                 :            :     {
     266                 :          2 :       g_test_add_func ("/message", test_message);
     267                 :            :     }
     268         [ +  + ]:          6 :   else if (g_strcmp0 (argv1, "print") == 0)
     269                 :            :     {
     270                 :          2 :       g_test_add_func ("/print", test_print);
     271                 :            :     }
     272         [ +  + ]:          4 :   else if (g_strcmp0 (argv1, "subprocess-stdout") == 0)
     273                 :            :     {
     274                 :          1 :       g_test_add_func ("/sub-stdout", test_subprocess_stdout);
     275                 :            :     }
     276         [ +  + ]:          3 :   else if (g_strcmp0 (argv1, "subprocess-stdout-no-nl") == 0)
     277                 :            :     {
     278                 :          1 :       g_test_add_func ("/sub-stdout-no-nl", test_subprocess_stdout_no_nl);
     279                 :            :     }
     280                 :            :   else
     281                 :            :     {
     282         [ +  - ]:          2 :       if (g_test_subprocess ())
     283                 :            :         {
     284                 :          2 :           g_test_add_func ("/sub-stdout", test_subprocess_stdout);
     285                 :          2 :           g_test_add_func ("/sub-stdout-no-nl", test_subprocess_stdout_no_nl);
     286                 :            :         }
     287                 :            :       else
     288                 :            :         {
     289                 :            :           g_assert_not_reached ();
     290                 :            :         }
     291                 :            :     }
     292                 :            : 
     293                 :         44 :   return g_test_run ();
     294                 :            : }

Generated by: LCOV version 1.14