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