[glib] glib/tests: use g_test_expect_message()



commit cc24dac3c8edbe2068ba15522bb5d937602082dc
Author: Dan Winship <danw gnome org>
Date:   Mon Jul 30 16:38:30 2012 -0400

    glib/tests: use g_test_expect_message()
    
    Replace some tests that used to use g_test_trap_fork() with
    g_test_expect_message() instead.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=679556

 glib/tests/Makefile.am      |    1 +
 glib/tests/error.c          |   45 +++++++-----
 glib/tests/gvariant.c       |   56 ++++++++-------
 glib/tests/mainloop.c       |   11 +++-
 glib/tests/markup-collect.c |   20 +++---
 glib/tests/strfuncs.c       |  163 ++++++++++++++++++++++---------------------
 6 files changed, 159 insertions(+), 137 deletions(-)
---
diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am
index 26ae31c..983259b 100644
--- a/glib/tests/Makefile.am
+++ b/glib/tests/Makefile.am
@@ -3,6 +3,7 @@ include $(top_srcdir)/Makefile.decl
 INCLUDES = 				\
 	-g 				\
 	$(glib_INCLUDES) 		\
+	-DG_LOG_DOMAIN=\"GLib\" 	\
 	-DSRCDIR=\""$(srcdir)"\"	\
 	$(GLIB_DEBUG_FLAGS)
 
diff --git a/glib/tests/error.c b/glib/tests/error.c
index be03cfd..81b4055 100644
--- a/glib/tests/error.c
+++ b/glib/tests/error.c
@@ -3,28 +3,35 @@
 static void
 test_overwrite (void)
 {
+  GError *error, *dest, *src;
+
   if (!g_test_undefined ())
     return;
 
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-    {
-      GError *error;
-      error = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY, "bla");
-      g_set_error_literal (&error, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "bla");
-    }
-  g_test_trap_assert_failed ();
-  g_test_trap_assert_stderr ("*set over the top*");
-
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-    {
-      GError *dest;
-      GError *src;
-      dest = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY, "bla");
-      src = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "bla");
-      g_propagate_error (&dest, src);
-    }
-  g_test_trap_assert_failed ();
-  g_test_trap_assert_stderr ("*set over the top*");
+  error = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY, "bla");
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                         "*set over the top*");
+  g_set_error_literal (&error, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "bla");
+  g_test_assert_expected_messages ();
+
+  g_assert_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY);
+  g_error_free (error);
+
+
+  dest = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY, "bla");
+  src = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "bla");
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                         "*set over the top*");
+  g_propagate_error (&dest, src);
+  g_test_assert_expected_messages ();
+
+  g_assert_error (dest, G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY);
+  g_assert_error (src, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE);
+  g_error_free (dest);
+  g_error_free (src);
+
 }
 
 static void
diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c
index a6bef23..d91b78f 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -2832,28 +2832,36 @@ do_failed_test (const gchar *pattern)
 static void
 test_invalid_varargs (void)
 {
+  GVariant *value;
+  const gchar *end;
+
   if (!g_test_undefined ())
     return;
 
-  if (do_failed_test ("*GVariant format string*"))
-    {
-      g_variant_new ("z");
-      abort ();
-    }
-
-  if (do_failed_test ("*valid GVariant format string as a prefix*"))
-    {
-      const gchar *end;
-
-      g_variant_new_va ("z", &end, NULL);
-      abort ();
-    }
-
-  if (do_failed_test ("*type of `q' but * has a type of `y'*"))
-    {
-      g_variant_get (g_variant_new ("y", 'a'), "q");
-      abort ();
-    }
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*GVariant format string*");
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*valid_format_string*");
+  value = g_variant_new ("z");
+  g_test_assert_expected_messages ();
+  g_assert (value == NULL);
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*valid GVariant format string as a prefix*");
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*valid_format_string*");
+  value = g_variant_new_va ("z", &end, NULL);
+  g_test_assert_expected_messages ();
+  g_assert (value == NULL);
+
+  value = g_variant_new ("y", 'a');
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*type of `q' but * has a type of `y'*");
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*valid_format_string*");
+  g_variant_get (value, "q");
+  g_test_assert_expected_messages ();
+  g_variant_unref (value);
 }
 
 static void
@@ -3133,12 +3141,10 @@ test_varargs (void)
         g_free (str);
       }
 
-  if (do_failed_test ("*NULL has already been returned*"))
-    {
-      g_variant_iter_next_value (&iter);
-      abort ();
-    }
-
+    g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                           "*NULL has already been returned*");
+    g_variant_iter_next_value (&iter);
+    g_test_assert_expected_messages ();
 
     while (g_variant_iter_loop (i3, "*", &sub))
       {
diff --git a/glib/tests/mainloop.c b/glib/tests/mainloop.c
index e162202..31bf72f 100644
--- a/glib/tests/mainloop.c
+++ b/glib/tests/mainloop.c
@@ -87,9 +87,18 @@ test_maincontext_basic (void)
   g_assert (g_source_get_context (source) == ctx);
   g_assert (g_main_context_find_source_by_id (ctx, id) == NULL);
 
-  g_source_unref (source);
   g_main_context_unref (ctx);
 
+  if (g_test_undefined ())
+    {
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*source->context != NULL*failed*");
+      g_assert (g_source_get_context (source) == NULL);
+      g_test_assert_expected_messages ();
+    }
+
+  g_source_unref (source);
+
   ctx = g_main_context_default ();
   source = g_source_new (&funcs, sizeof (GSource));
   g_source_set_funcs (source, &funcs);
diff --git a/glib/tests/markup-collect.c b/glib/tests/markup-collect.c
index 65cc67b..6c88a93 100644
--- a/glib/tests/markup-collect.c
+++ b/glib/tests/markup-collect.c
@@ -238,22 +238,20 @@ static GMarkupParser cleanup_parser = {
 static void
 test_cleanup (void)
 {
+  GMarkupParseContext *context;
+
   if (!g_test_undefined ())
     return;
 
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-    {
-      GMarkupParseContext *context;
+  context = g_markup_parse_context_new (&cleanup_parser, 0, NULL, NULL);
+  g_markup_parse_context_parse (context, XML, -1, NULL);
 
-      context = g_markup_parse_context_new (&cleanup_parser, 0, NULL, NULL);
-      g_markup_parse_context_parse (context, XML, -1, NULL);
-      g_markup_parse_context_end_parse (context, NULL);
-      g_markup_parse_context_free (context);
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "assertion `context->state != STATE_ERROR' failed");
+  g_markup_parse_context_end_parse (context, NULL);
+  g_test_assert_expected_messages ();
 
-      exit (0);
-    }
-  g_test_trap_assert_failed ();
-  g_test_trap_assert_stderr ("*assertion `context->state != STATE_ERROR' failed*");
+  g_markup_parse_context_free (context);
 }
 
 int
diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c
index 8e376f7..f0ea4a2 100644
--- a/glib/tests/strfuncs.c
+++ b/glib/tests/strfuncs.c
@@ -329,19 +329,21 @@ test_strcanon (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strcanon (NULL, "ab", 'y');
-        }
-      g_test_trap_assert_failed ();
-
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strdup ("abxabxab");
-          str = g_strcanon (str, NULL, 'y');
-          g_free (str);
-        }
-      g_test_trap_assert_failed ();
+      gchar *ret;
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      str = g_strcanon (NULL, "ab", 'y');
+      g_test_assert_expected_messages ();
+      g_assert (str == NULL);
+
+      str = g_strdup ("abxabxab");
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      ret = g_strcanon (str, NULL, 'y');
+      g_test_assert_expected_messages ();
+      g_assert (ret == NULL);
+      g_free (str);
     }
 
   str = g_strdup ("abxabxab");
@@ -360,18 +362,19 @@ test_strcompress_strescape (void)
   /* test compress */
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strcompress (NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      str = g_strcompress (NULL);
+      g_test_assert_expected_messages ();
+      g_assert (str == NULL);
 
       /* trailing slashes are not allowed */
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strcompress ("abc\\");
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                             "*trailing \\*");
+      str = g_strcompress ("abc\\");
+      g_test_assert_expected_messages ();
+      g_assert_cmpstr (str, ==, "abc");
+      g_free (str);
     }
 
   str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
@@ -382,11 +385,11 @@ test_strcompress_strescape (void)
   /* test escape */
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strescape (NULL, NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      str = g_strescape (NULL, NULL);
+      g_test_assert_expected_messages ();
+      g_assert (str == NULL);
     }
 
   str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
@@ -416,17 +419,17 @@ test_ascii_strcasecmp (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_ascii_strcasecmp ("foo", NULL);
-        }
-      g_test_trap_assert_failed ();
-
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_ascii_strcasecmp (NULL, "foo");
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_ascii_strcasecmp ("foo", NULL);
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_ascii_strcasecmp (NULL, "foo");
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
     }
 
   res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
@@ -492,11 +495,10 @@ test_strchug (void)
 {
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          g_strchug (NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_strchug (NULL);
+      g_test_assert_expected_messages ();
     }
 
   do_test_strchug ("", "");
@@ -528,11 +530,10 @@ test_strchomp (void)
 {
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          g_strchomp (NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_strchomp (NULL);
+      g_test_assert_expected_messages ();
     }
 
   do_test_strchomp ("", "");
@@ -552,11 +553,11 @@ test_strreverse (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          str = g_strreverse (NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      str = g_strreverse (NULL);
+      g_test_assert_expected_messages ();
+      g_assert (str == NULL);
     }
 
   str = p = g_strdup ("abcde");
@@ -656,17 +657,17 @@ test_has_prefix (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_str_has_prefix ("foo", NULL);
-        }
-      g_test_trap_assert_failed ();
-
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_str_has_prefix (NULL, "foo");
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_str_has_prefix ("foo", NULL);
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_str_has_prefix (NULL, "foo");
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
     }
 
   res = g_str_has_prefix ("foo", "bar");
@@ -698,17 +699,17 @@ test_has_suffix (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_str_has_suffix ("foo", NULL);
-        }
-      g_test_trap_assert_failed ();
-
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          res = g_str_has_suffix (NULL, "foo");
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_str_has_suffix ("foo", NULL);
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      res = g_str_has_suffix (NULL, "foo");
+      g_test_assert_expected_messages ();
+      g_assert (res == FALSE);
     }
 
   res = g_str_has_suffix ("foo", "bar");
@@ -880,11 +881,11 @@ test_strv_length (void)
 
   if (g_test_undefined ())
     {
-      if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
-        {
-          l = g_strv_length (NULL);
-        }
-      g_test_trap_assert_failed ();
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      l = g_strv_length (NULL);
+      g_test_assert_expected_messages ();
+      g_assert_cmpint (l, ==, 0);
     }
 
   strv = g_strsplit ("1,2,3,4", ",", -1);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]